ORA - 01847

Hi,
Can anyone help me with this error?
ORA - 01847: day of month must be between 1 and last day of month
SELECT CLIENT_ID, SUM(UPDATE_QTY) SUMA
FROM inventory_transaction ITL
WHERE CODE = 'Adjustment'
AND ITL.DSTAMP BETWEEN TO_DATE($P{from_date} || ' ' || $P{from_time}, 'DD-Mon-YYYY HH24') AND TO_DATE($P{to_date} || ' ' || $P{to_time}, 'DD-Mon-YYYY HH24')
group by client_idRegards,
Sam.

Hi, Sam,
Either from_date or to_date is an impossible date, such as February 31.
See [this thread|http://forums.oracle.com/forums/thread.jspa?messageID=3878849] for a function that can detect invalid dates without stopping your query.
If you need more help, post a complete test case (that is, something people can run an get the same error) and the results you want.
For example:
"I have this table: CREATE TABLE ...
with this data: INSERT ...
This query: SELECT ... works fine as long as the user enters a valid :from_date
But if the user gives an invalid :from_date like ...
then I want to get these results: ..."

Similar Messages

  • SQL   Loader and Error ORA-01847/ORA-01839

    Hi,
    While using the direct loading in SQL-LOADER when we get the ORA-01847/ORA-01839 all the other records are getting errorred out. It goes fine with the conventional loading.
    Should I use some parameters or anything to make sure that all the other records are not rejected when we get the ORA-01847/ORA-01839 error while going with the DIRECT loading.
    Thanks
    Jibin

    In internet I found this short message:
    “AL32UTF8 is a multi-byte characterset,that means some characters are stored in more than 1 character, that's true for these special characters.
    If you have same table definitions in both databases you likely face error ORA-12899.
    This metalink note discusses this problem, it's also applicable to sqlloader:
    Import reports "ORA-12899: Value too large for column" when using BYTE semantic
    Doc ID: Note:563893.1”
    By metalink, I can see the Note linked to a one Oracle Internal Bug for Oracle 11g.....
    I'm waiting you suggestion... thanks very much in advance.
    Regards.
    Giovanni

  • Error: ORA-01847: day of month must be between 1 and last day of month

    Hi,
    I am getting this ORA-01847: day of month must be between 1 and last day of month type of error but I have checked my data all are correct.
    I am inserting the data in the merge query. It is giving the above error.
    but when i insert the data like that
    insert into dt(start_date) select TO_DATE (tariff_start_date, 'DD/MM/RRRR') from ext_zpp0a871;
    It does not give any error.
    Please find the below code
    DECLARE
    l_sv_error_msg VARCHAR2 (1000);
    BEGIN
    FOR i IN (SELECT condition_type, sales_org, division_channel, division,
    price_list, ean_upc, amount, unit_of_measure1,
    tariff_start_date, tariff_end_date
    FROM ext_zpp0a871--This is external table
    WHERE condition_type = 'ZPP0' AND ROWNUM < 200)
    LOOP
    BEGIN
    MERGE INTO sap_tariff st
    USING (SELECT i.price_list pl, i.ean_upc upc,
    TO_DATE (i.tariff_start_date, 'DD/MM/RRRR') tsd,
    TO_DATE (i.tariff_end_date, 'DD/MM/RRRR') ted
    FROM DUAL) du
    ON (st.prod_ean_cuni = du.upc
    AND st.pricelist = du.pl
    AND st.tariff_start_date = du.tsd
    AND st.tariff_end_date = du.ted)
    WHEN MATCHED THEN
    UPDATE
    SET st.condition_type = i.condition_type,
    st.sales_org = i.sales_org,
    st.division_channel = i.division_channel,
    st.division = i.division,
    st.amount =
    TO_NUMBER (REPLACE (REPLACE (i.amount, '.', NULL),
    st.uom = i.unit_of_measure1
    WHEN NOT MATCHED THEN
    INSERT (condition_type, sales_org, division_channel, division,
    pricelist, prod_ean_cuni, amount, uom,
    tariff_start_date, tariff_end_date)
    VALUES (i.condition_type, i.sales_org, i.division_channel,
    i.division, i.price_list, i.ean_upc,
    TO_NUMBER (REPLACE (REPLACE (i.amount, '.', NULL),
    i.unit_of_measure1,
    TO_DATE (i.tariff_start_date, 'DD/MM/RRRR'),
    TO_DATE (i.tariff_end_date, 'DD/MM/RRRR'));
    /*INSERT INTO sap_tariff
    (condition_type, sales_org, division_channel,
    division, pricelist, prod_ean_cuni,
    amount,
    uom,
    tariff_start_date,
    tariff_end_date
    VALUES (i.condition_type, i.sales_org, i.division_channel,
    i.division, i.price_list, i.ean_upc,
    TO_NUMBER (REPLACE (REPLACE (i.amount, '.', NULL),
    i.unit_of_measure1,
    TO_DATE (i.tariff_start_date, 'DD/MM/RRRR'),
    TO_DATE (i.tariff_end_date, 'DD/MM/RRRR')
    EXCEPTION
    WHEN DUP_VAL_ON_INDEX
    THEN
    UPDATE sap_tariff
    SET condition_type = i.condition_type,
    sales_org = i.sales_org,
    division_channel = i.division_channel,
    division = i.division,
    amount =
    TO_NUMBER (REPLACE (REPLACE (i.amount, '.', NULL),
    uom = i.unit_of_measure1;
    WHEN OTHERS
    THEN
    l_sv_error_msg := SQLERRM (SQLCODE);
    INSERT INTO sap_tariff_log
    (date_of_load, condition_type, sales_org,
    division_channel, division, price_list,
    prod_ean_cuni, amount, uom,
    tariff_start_date, tariff_end_date,
    rejection_reason
    VALUES (SYSDATE, i.condition_type, i.sales_org,
    i.division_channel, i.division, i.price_list,
    i.ean_upc, i.amount, i.unit_of_measure1,
    i.tariff_start_date, i.tariff_end_date,
    l_sv_error_msg
    END;
    END LOOP;
    COMMIT;
    EXCEPTION
    WHEN OTHERS
    THEN
    l_sv_error_msg := SQLERRM (SQLCODE);
    neo_pro_log ('Others Error', l_sv_error_msg);
    END;

    user13400510 wrote:
    but when i insert the data like that
    insert into dt(start_date) select TO_DATE (tariff_start_date, 'DD/MM/RRRR') from ext_zpp0a871;
    It does not give any error.Maybe the error is on tariff_end_date instead of tariff_start_date ?
    What are the datatypes of tariff_start_date and tariff_end_date ? If they are already dates, and you to_date them (and depending on what your nls_date_xxxxx params are) you can have such errors :SQL> sho parameter nls_date_format
    NAME_COL_PLUS_SHOW_PARAM                 TYPE        VALUE_COL_PLUS_SHOW_PARAM
    nls_date_format                          string      DD/MM/YYYY
    SQL> select to_date(sysdate,'Day dd Mon RRRR') from dual;
    select to_date(sysdate,'Day dd Mon RRRR') from dual
    ERROR at line 1:
    ORA-01846: not a valid day of the week
    SQL> select to_date(sysdate,'hh24:mi:ss dd/mm/rrrr') from dual;
    select to_date(sysdate,'hh24:mi:ss dd/mm/rrrr') from dual
    ERROR at line 1:
    ORA-01850: hour must be between 0 and 23

  • ORA-01847: day of month must be between 1 and last day of month error

    Hi All
    When i run the following code i'm getting the error:ORA-01847: day of month must be between 1 and last day of month error
    SELECT t2.owner_cat,
    t1.owner_id,
    stock_id,
    Sum(received_amount) received_amount,
    Sum(curr_amount) curr_amount,
    Sum(used_amount) used_amount,
    To_char(Add_months(date_work,6),'YYYY') date_work
    FROM (SELECT owner_id_to owner_id,
    stock_id_to stock_id,
    Sum(full_amount) received_amount,
    0 curr_amount,
    0 used_amount,
    To_char(0) date_work
    FROM dw_wms_mat_transfer
    WHERE master_code_id IN ('8664','8665')
    GROUP BY owner_id_to,
    stock_id_to
    UNION ALL
    SELECT owner_id,
    stock_id,
    0 received_amount,
    Sum(curr_amount) curr_amount,
    0 used_amount,
    To_char(0) date_work
    FROM dw_wms_mat_inv
    WHERE master_code_id IN ('8664','8665')
    GROUP BY owner_id,
    stock_id
    UNION ALL
    SELECT t2.owner_id,
    t1.stock_id,
    0 received_amt,
    0 curr_amt,
    Sum(amount) used_amt,
    To_char(Add_months(date_work,6),'YYYY') date_work
    FROM dw_wms_wo_mat_dc t1,
    (SELECT owner_id,
    stock_id
    FROM dw_wms_mat_inv
    WHERE master_code_id IN ('8664','8665')
    GROUP BY owner_id,
    stock_id) t2
    WHERE t1.stock_id = t2.stock_id
    GROUP BY t2.owner_id,
    t1.stock_id,
    To_char(Add_months(date_work,6),'YYYY')) t1,
    dw_wms_setup_owner t2
    WHERE t1.owner_id = t2.owner_id
    GROUP BY t2.owner_cat,
    t1.owner_id,
    stock_id,
    To_char(Add_months(date_work,6),'YYYY')
    But when i just run the inner query:
    (SELECT owner_id_to owner_id,
    stock_id_to stock_id,
    Sum(full_amount) received_amount,
    0 curr_amount,
    0 used_amount,
    To_char(0) date_work
    FROM dw_wms_mat_transfer
    WHERE master_code_id IN ('8664','8665')
    GROUP BY owner_id_to,
    stock_id_to
    UNION ALL
    SELECT owner_id,
    stock_id,
    0 received_amount,
    Sum(curr_amount) curr_amount,
    0 used_amount,
    To_char(0) date_work
    FROM dw_wms_mat_inv
    WHERE master_code_id IN ('8664','8665')
    GROUP BY owner_id,
    stock_id
    UNION ALL
    SELECT t2.owner_id,
    t1.stock_id,
    0 received_amt,
    0 curr_amt,
    Sum(amount) used_amt,
    To_char(Add_months(date_work,6),'YYYY') date_work
    FROM dw_wms_wo_mat_dc t1,
    (SELECT owner_id,
    stock_id
    FROM dw_wms_mat_inv
    WHERE master_code_id IN ('8664','8665')
    GROUP BY owner_id,
    stock_id) t2
    WHERE t1.stock_id = t2.stock_id
    GROUP BY t2.owner_id,
    t1.stock_id,
    To_char(Add_months(date_work,6),'YYYY'))
    I'm not getting that error, any help please?

    Hi,
    Like Anuraq already explained, you need to keep in mind that when you apply a to_char, you've turned your DATE datatype into a STRING and by doing that you've lost the ability to use the add_months function again in your outer query.
    Using a simplified example, extracted from your example:
    MHO%xe> select to_char(add_months(date_work, 6), 'yyyy') date_work
      2  from ( select to_char(0) date_work
      3         from dual
      4         union all
      5         select to_char(0) date_work
      6         from dual
      7         union all
      8         select to_char(add_months(sysdate, 6), 'yyyy') date_work
      9         from dual
    10       );
    select to_char(add_months(date_work, 6), 'yyyy') date_work
    FOUT in regel 1:
    .ORA-01847: day of month must be between 1 and last day of monthHowever, you should either do something like:
    MHO%xe> select nvl(to_char(add_months(date_work, 12), 'yyyy'), '0') date_work
      2  from ( select to_date(null) date_work
      3         from dual
      4         union all
      5         select to_date(null) date_work
      6         from dual
      7         union all
      8         select sysdate date_work
      9         from dual
    10       );
    DATE
    0
    0
    2010or (less clear, more confusing):
    MHO%xe> select nvl(to_char(add_months(date_work, 6), 'yyyy'), '0') date_work
      2  from ( select to_date(null) date_work
      3         from dual
      4         union all
      5         select to_date(null) date_work
      6         from dual
      7         union all
      8         select add_months(sysdate, 6) date_work
      9         from dual
    10       );
    DATE
    0
    0
    2010It's not clear to me why you're using add_months twice (inner + outer query) in your original example.
    As you can see you can get the same results using it only once in your outer query.

  • Ora-01847 error (day of month must be between 1 and last day of month)

    Hi
    Executng the below query, I am getting error ora-01847
       INSERT INTO SVC_ORDER
          (DUE_DATE)                   
           SELECT to_date(lpad(decode(O.SVC_ORD_DT_REQ,999999,null,O.SVC_ORD_DT_REQ),6,'0'),'rrmmdd') AS DUE_DT
                  FROM M04_NT_UV_ORD_ADMIN O

    The query is from the thread I linked above
    WITH m04_nt_uv_ord_admin AS (
         SELECT '999999' svc_ord_dt_req FROM dual UNION ALL
         SELECT '120215' svc_ord_dt_req FROM dual UNION ALL
         SELECT '1231' svc_ord_dt_req FROM dual UNION ALL
         SELECT '1232' svc_ord_dt_req FROM dual UNION ALL
         SELECT '514208' svc_ord_dt_req FROM dual UNION ALL
         SELECT '120314' svc_ord_dt_req FROM dual UNION ALL
         SELECT '121312' svc_ord_dt_req FROM dual UNION ALL
         SELECT '0' svc_ord_dt_req FROM dual
    SELECT CASE WHEN
                CASE WHEN TO_NUMBER(SUBSTR(LPAD(o.svc_ord_dt_req,6,'0'),3,2))
                  NOT BETWEEN 1 AND 12
                THEN
                    'error'
                WHEN TO_NUMBER(SUBSTR(LPAD(o.svc_ord_dt_req,6,'0'),5,2))
                  NOT BETWEEN 1
                      AND TO_NUMBER(
                           TO_CHAR(
                            LAST_DAY(
                             TO_DATE(
                              SUBSTR(LPAD(o.svc_ord_dt_req,6,'0'),1,4)
                              ,'rrmm')
                            ,'dd'))
                THEN
                    'error'
                END IS NOT NULL
            THEN
                NULL
            ELSE
                TO_DATE(LPAD(o.svc_ord_dt_req,6,'0'),'rrmmdd')
            END check_date
           ,o.svc_ord_dt_req
    FROM    m04_nt_uv_ord_admin o
    WHERE   o.svc_ord_dt_req != 999999;
    CHECK_DATE SVC_ORD_DT_REQ
    15.02.2012 120215        
    31.12.2000 1231          
               1232          
               514208        
    14.03.2012 120314        
               121312        
               0  Regards
    Marcus

  • Ora-01847 day of month between 1 and last date of month coming in my query

    i m executing simple query but it is not working plz do help
    select *
    from sgvf_tree_nodes_vw
    where to_date(node_attr9_val, 'dd/mm/yyyy') between
    to_date('01/01/2010', 'dd/mm/yyyy') and
    to_date('31/12/2010', 'dd/mm/yyyy')
    gives : Ora-01847 day of month should be between 1 and last date of month

    Hi,
    Check the date format's once
    SQL> drop table demo;
    Table dropped.
    SQL> create table demo(val number, todate varchar2(12));
    Table created.
    SQL> insert into demo values(1,'04/11/2008');
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select * from demo;
           VAL TODATE
             1 04/11/2008
    SQL> select sysdate from dual;
    SYSDATE
    03-FEB-11
    SQL> insert into demo values(1,'04/11/2010');
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> select *
      2  from demo
      3  where to_date(todate,'dd/mm/yyyy') between to_date('01/01/2010', 'dd/mm/yyyy') and to_date('31/12/2010', 'dd/m
    m/yyyy');
           VAL TODATE
             1 04/11/2010- Pavan Kumar N

  • Oracle Trigger - How can I capture and correct on the fly  ORA-01847

    Oracle Trigger - How can I capture and correct on the fly ORA-01847 day of month must be between 1.
    I have a table that has a date column. Sometimes an invalid date is passed and I'd like to capture and initilize the incoming value to null and have it inserted without error. Currently I get ORA-01847 day of month must be between 1.
    Example:
    create table ep1
    col_1 number,
    col_2 date
    Now if a user does the following, I want no error to occure but rather for the date value to be set to null and inserted.
    insert into ep1 values('1', '41-JAN-13')
    The insert may come from any client and I can't change the calling code hence can't used a precedure to replace the insert. I'm guessing there is some way to catch system level exceptions on the DB level. This doesn't need to be nice, it just needs to work.
    Here is the question on experts exchange. I'm told there is no way, but there must be. After all the exception is on a DB system level and I would think one could extend or override these, or disable them.
    http://www.experts-exchange.com/Database/Oracle/PL_SQL/Q_23045306.html

    SQL> declare
      2     wrong_month      exception;
      3     pragma exception_init (wrong_month, -1843);
      4  begin
      5    begin
      6      insert into emp (empno, hiredate)
      7      values (2345,to_date('13/25/81','MM/DD/YY'));
      8    exception
      9      when wrong_month then
    10      dbms_output.put_line('I caught the bad month');
    11      insert into emp (empno, hiredate)
    12      values (2345,null);
    13    end;
    14  end;
    15  /
    I caught the bad month
    PL/SQL procedure successfully completed.
    SQL>
    SQL> select * from emp
      2  where empno = 2345;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
          2345Note the 1843 error vice 1847. It can be done outside of a trigger.

  • Ora-29740 problem

    hi,
    i am oracle dba. i am having database of oracle 9.2.0.6 with 2 node RAC on IBM AIX 5.2. recently from past 1 week i am getting error ora-29740 and my 2nd instance is terminating by LMON. when i checked, it says may be communication problem between clusters or heartbeat failure to control file.
    can anyone please give me more details about this.and if possible solution also
    thanks in advance.

    as Andrew had mentioned you are trying to convert a numeric into date that might not be in date format. for example you had a value of 482334
    SQL> select substr(ltrim('482334'),1,6)
      2    from dual;
    SUBSTR
    482334then you tried to convert it into date format
    SQL> select to_date(substr(ltrim('482334'),1,6),'ddmmyyyy')
      2    from dual;
    select to_date(substr(ltrim('482334'),1,6),'ddmmyyyy')
    ERROR at line 1:
    ORA-01847: day of month must be between 1 and last day of month
    SQL> it will give you an error. what is the datatype for your column PRTCRC.CRCID?

  • ORA-22812 problem

    Hi -
    We recently are experiencing ORA-22812 exception quite often, most of time it points to a specific table (sometimes point to a different table), as if this table was totally crashed.
    What puzzles me is that we did not use any nested table in these problematic tables. Is there any other possible causes for this ORA-22812 exception?
    We are using Oracle 9i
    Any suggested will be appreciated!
    WMC

    as Andrew had mentioned you are trying to convert a numeric into date that might not be in date format. for example you had a value of 482334
    SQL> select substr(ltrim('482334'),1,6)
      2    from dual;
    SUBSTR
    482334then you tried to convert it into date format
    SQL> select to_date(substr(ltrim('482334'),1,6),'ddmmyyyy')
      2    from dual;
    select to_date(substr(ltrim('482334'),1,6),'ddmmyyyy')
    ERROR at line 1:
    ORA-01847: day of month must be between 1 and last day of month
    SQL> it will give you an error. what is the datatype for your column PRTCRC.CRCID?

  • Ora-01843 problem

    My code is giving me ora-01843 problem. When i put startdate as 01-jan-05 and
    enddate as 31-jan-05, it gives me error. Please help me guys.
    --declare
    --ntwk_var varchar2(100);
    SELECT PHY.PHYNAM,
    decode (NETWORKPROTOCOL.LCC, 'Y', '<Lombardi Cancer Center>', '') ||
    decode (NETWORKPROTOCOL.UMH, 'Y', ' <Union Memorial Hospital>', '') ||
    decode (NETWORKPROTOCOL.GSH, 'Y', '<Good Somaritan Hospital>', '') ||
    decode (NETWORKPROTOCOL.FSH, 'Y', '<Franklin Square Hospital>', '') ||
    decode (NETWORKPROTOCOL.WHC, 'Y', '<Washington Hospital Center>', '') ||
    decode (NETWORKPROTOCOL.HH, 'Y', '<Harbor Hospital>', '') as NETWORKID,
    PRTCRC.FULLNAME,
    CRCMTG.CRMTGDT, CRCMTG.CRCSTAT, PRTCRC.SUBMITTEDTOIRB,
    PRTCRC.RECEIVEDIRBRESULTDATE,
    PRTCRC.PRT, prtcrc.crcid,
    PRTCRC.REVIEWSTATUS,
    CRCMTG.CRCOUTCOME,
    PRTCRC.REVIEWTYPE,
    PRTCRC.MODIFICATIONDATE, PRTCRC.FINALIRBAPPROVALDATE,
    PRTCRC.ACTIVATIONDATE, RECENTREAPPROVALDATE.RECENTREAPPROVAL,
    recentamendmentdate.amenddate,
    PRTCRC.CLOSURETOPATIENTACCRUAL, PRTCRC.TERMINATIONDATE,
    prttype.descrip
    FROM CRCMTG, PRTCRC, NETWORKPROTOCOL, RECENTREAPPROVALDATE, PHY, prttype, recentamendmentdate
    WHERE
    ((CRCMTG.PRTCRCID(+)=PRTCRC.ID) AND
    (NETWORKPROTOCOL.PRTCRCID(+)=PRTCRC.ID) AND
    (PRTCRC.PRINCIPALINVESTIGATOR=PHY.PHY(+))
    AND (PRTCRC.ID=RECENTREAPPROVALDATE.PRTCRCID(+))
    AND (PRTCRC.ID=recentamendmentdate.PRTCRCID(+))
    ) and
    TO_DATE('01'||SUBSTR(LTRIM( NVL(PRTCRC.CRCID, TO_CHAR(sysdate, 'MMYYYY'))) , 1, 6), 'DDMMYYYY') >= :startdate
    and
    TO_DATE('01'||SUBSTR(LTRIM( NVL(PRTCRC.CRCID, TO_CHAR(sysdate, 'MMYYYY'))) , 1, 6), 'DDMMYYYY') <= :enddate
    ) and
    prtcrc.type=prttype.id(+) and
    fullname is not null
    order by &SORT

    as Andrew had mentioned you are trying to convert a numeric into date that might not be in date format. for example you had a value of 482334
    SQL> select substr(ltrim('482334'),1,6)
      2    from dual;
    SUBSTR
    482334then you tried to convert it into date format
    SQL> select to_date(substr(ltrim('482334'),1,6),'ddmmyyyy')
      2    from dual;
    select to_date(substr(ltrim('482334'),1,6),'ddmmyyyy')
    ERROR at line 1:
    ORA-01847: day of month must be between 1 and last day of month
    SQL> it will give you an error. what is the datatype for your column PRTCRC.CRCID?

  • Query to pick up people of retirement age 1 year in advance

    Hi,
    I wrote the below query to pick up all staff who will be of retirement age in a year's time. This will be used in an alert that must go to payroll 1 year before retirement date, then 6 months before retirement date and then finally 1 month before retirement date.
    select distinct papf.employee_number
    , papf.full_name
    , apps.meds_hr_util_pkg.return_department(papf.person_id) org
    , papf.date_of_birth
    , (select payroll_name from pay_all_payrolls_f where payroll_id = paaf.payroll_id) payroll_name
    , (select first_name||' '||last_name from per_all_people_f where person_id = paaf.supervisor_id and trunc(sysdate) between effective_start_date and effective_end_date) mgr
    , (select email_address from per_all_people_f where person_id = paaf.supervisor_id and trunc(sysdate) between effective_start_date and effective_end_date) mgr_email
    , add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') ) ||to_char(sysdate, '-YYYY')),-12) date_12month_advance
    , add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') ) ||to_char(sysdate, '-YYYY')),-6) date_6month_advance
    , add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') ) ||to_char(sysdate, '-YYYY')),-1) date_1month_advance
    , trunc(months_between(add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') ) ||to_char(sysdate, '-YYYY')),-12),papf.date_of_birth)/12) age_12mon
    --into &emp_no, &emp_name, &org, &date_of_birth, &payroll_name, &mgr, &mgr_email, &retirement_date     
    from per_all_people_f papf
    ,per_all_assignments_f paaf
    ,per_person_type_usages_f pptuf
    ,per_person_types ppt
    where papf.person_id = paaf.person_id
    and trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
    and trunc(sysdate) between paaf.effective_start_date and paaf.effective_end_date
    and paaf.primary_flag = 'Y'
    and paaf.assignment_type = 'E'
    and papf.person_id = pptuf.person_id
    and pptuf.person_type_id = ppt.person_type_id
    and trunc(sysdate) between pptuf.effective_start_date and pptuf.effective_end_date
    and ppt.system_person_type = 'EMP'
    and ppt.user_person_type != 'Pensioners'
    and trunc(months_between(add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') )
    ||to_char(sysdate, '-YYYY')),-12),papf.date_of_birth)/12) >= 62
    and trunc(sysdate) in (add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') ) ||to_char(sysdate, '-YYYY')),-12)
    , add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') ) ||to_char(sysdate, '-YYYY')),-6)
    , add_months(to_date(DECODE(to_char(papf.date_of_birth, 'DD-MON'), '29-FEB', '28-FEB', to_char(papf.date_of_birth, 'DD-MON') ) ||to_char(sysdate, '-YYYY')),-1)
    and to_char(papf.date_of_birth, 'DD-MON') != '29-FEB'
    order by 1
    The query works only when I have this line in
    " and to_char(papf.date_of_birth, 'DD-MON') != '29-FEB' "
    As soon as I remove it I get an ORA-01847: day of month must be between 1 and last day of month error. I can't seem to figure out why because there is 1 person with a birth date of 29-FEB and I am catering for this in the query. Also, I am using TO_DATE for the values so using add_months and months_between should not be an issue.
    Please help....maybe someone else can see something I am missing...
    Thanks
    Shalantha

    Hi,
    To find people who are 65 years old (or older) as of today, you can simply say
    WHERE     date_of_birth <= ADD_MONTHS ( SYSDATE
                                       , -12 * 65
                                )ADD_MONTHS knows how to adjust for leap years, but it may not adjust for them the way you expect.
    If today is February 28, 2013, the condition above will include people born on Februry 29, 1948. That's actually 1 day before they reach 65. If that's a problem, the solution is slightly more complicated.
    If today is March 1, 2013, it will include people born on Februry 29, 1948.
    If today is February 29, 2012 (or February 28, 2012) it will include people born on February 28, 1947, but not people born on March 1, 1947.
    To find people who will be 65 years old (or over) 3 months from today:
    WHERE     date_of_birth <= ADD_MONTHS ( SYSDATE
                                       , (-12 * 65) + 3
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data. Simplify the problem as much as possible.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.
    Edited by: Frank Kulash on May 26, 2011 11:48 AM

  • How to catch date errors and continue processing in a PL/SQL procedure

    I'm updating a date field with dates constructed from day, month and year fields. The incoming data has many instances of day and month that are not valid dates, ex 11 31 2007. There is no 31st day in November.
    I would like to write a pl/sql script to scan the table containing these values and log the rows that produce conversion errors.
    I thought I could do this with exceptions but there are no exceptions that correspond to the ORA-01847 error for mismatched day and month.
    Here is what I tried (the print procedure is a local wrapper for DBMS_OUTPUT.put_line):
    PROCEDURE date_check IS
    start1 DATE ;
    BEGIN
    select to_date(nvl(yearcollected,'9999') ||'/'|| nvl(monthcollected,'01') ||'/'|| nvl(daycollected,'01'),'YYYY/MM/DD'))) into start1 from incoming_data where id=1 ;
         BEGIN
              update temp_test set test_date = start1 where id=1 ;
         EXCEPTION
              WHEN OTHERS THEN
              print('Date error message from exception block');
         END;
    print('Processing continues after handling date exception') ;
    END date_check ;
    Is there a way to catch this kind of error and continue processing after logging a message?
    -=beeky

    Hi, Beeky,
    There are lots of different error messages associated with bad dates. Rather than try to catch them all, I use a BEGIN ... EXCEPTION block that contains nothing but a TO_DATE call. This is one of the rare occassions when I think "EXCEPTION WHEN OTHERS" is okay,
    The following function comes from a package. If you want to make a stand-alone function, remember to say " *CREATE OR REPLACE* FUNCTION ...".
    --          **   t o _ d t   **
    --     to_dt attempts to convert in_txt (assumed to
    --          be in the format of in_fmt_txt) to a DATE.
    --     If the conversion works, to_dt returns the DATE.
    --     If the conversion fails for any reason, to_dt returns in_err_dt.
    FUNCTION     to_dt
    (     in_txt          IN     VARCHAR2                    -- to be converted
    ,     in_fmt_txt     IN     VARCHAR2     DEFAULT     'DD-MON-YYYY'     -- optional format
    ,     in_err_dt     IN     DATE          DEFAULT     NULL
    RETURN DATE
    DETERMINISTIC
    AS
    BEGIN
         -- Try to convert in_txt to a DATE.  If it works, fine.
         RETURN     TO_DATE (in_txt, in_fmt_txt);
    EXCEPTION     -- If TO_DATE caused an error, then this is not a valid DATE: return in_err_dt
         WHEN OTHERS
         THEN
              RETURN in_err_dt;
    END     to_dt
    ;

  • External Tables Date Error

    I get error while loading data into Oracle 11g R2 with EXTERNAL TABLES.
    error processing column DATE_M in row 1 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01847: day of month must be between 1 and last day of month
    error processing column DATE_M in row 2 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month
    error processing column DATE_M in row 3 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month
    error processing column DATE_M in row 4 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01847: day of month must be between 1 and last day of month
    error processing column DATE_M in row 5 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month
    error processing column DATE_M in row 6 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month
    error processing column DATE_M in row 7 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month
    error processing column DATE_M in row 8 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month
    error processing column DATE_M in row 9 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month
    error processing column DATE_M in row 10 for datafile C:\app\S\admin\orcl\dpdump\TABLE_EXT.txt
    ORA-01843: not a valid month-----
    CREATE TABLE TABLE_EXT
       (  "COMPANY" VARCHAR2(101),
      "COMPANY_VN" VARCHAR2(15),
      "IL" VARCHAR2(17),
      "TERMINAL" VARCHAR2(8),
      "T_NO" VARCHAR2(15),
      "NAME" VARCHAR2(108),
      "SNAME" VARCHAR2(50),
      "REF_NO" VARCHAR2(23),
      "AMOUNT" NUMBER(15,2),
      "DATE_M" DATE,
      "TIME" VARCHAR2(11),
      "TEL_NO" VARCHAR2(25),
      "ADDRESS" VARCHAR2(50)
       ORGANIZATION EXTERNAL (
      TYPE ORACLE_LOADER
      DEFAULT DIRECTORY data_pump_dir
      ACCESS PARAMETERS (
        RECORDS DELIMITED BY NEWLINE
        FIELDS TERMINATED BY '|'
        MISSING FIELD VALUES ARE NULL
    COMPANY,
    COMPANY_VN,
    IL,
    TERMINAL,
    T_NO,
    NAME,
    SNAME,
    REF_NO,
    AMOUNT decimal  ,
    DATE_M CHAR  date_format DATE mask "dd.mm.yyyy" ,
    TIME,
    TEL_NO,
    ADDRESS
      LOCATION ('TABLE_EXT.txt')
    REJECT LIMIT 10------
    Sample Data:
    CITY HOSPITAL|04680072124|CITY|00614860|47746244218|JOHN|WHITE|172871|420,12|21.08.2011|14:26|0806422627784|06
    CITY HOSPITAL|04680072124|CITY|00614847|14274017676|BRAD|BROWN|448127|810,00|22.08.2011|11:04|0806427488476|06
    CITY HOSPITAL|04680072124|CITY|00614842|16218778886|PETER|BALSON|862626|12,00|24.08.2011|14:16|0806062177008|06
    CITY HOSPITAL|04680072124|CITY|00614846|14607666866|GEORGE|LOUIS|688811|40,10|24.08.2011|08:48|0806424172468|06
    CITY HOSPITAL|04680072124|CITY|00614846|14607666866|GEORGE|LOUIS|460481|42,64|24.08.2011|08:47|0806424172468|06
    CITY HOSPITAL|04680072124|CITY|00614860|18460662462|JR|TEPE|404622|44,16|22.04.2011|20:08|0806446446866|06
    CITY HOSPITAL|04680072124|CITY|00614840|47207688618|BARRY|HRAN|402886|42,40|27.08.2011|11:12|0806478768007|06
    CITY HOSPITAL|04680072124|CITY|00614847|42161048612|TOM|HIGGS|148640|12,00|06.06.2011|08:18|0806068076700|06
    CITY HOSPITAL|04680072124|CITY|00614846|42161048612|TOM|HIGGS|208847|12,00|06.06.2011|08:46|0806068076700|06-----
    NLS_DATE_FORMAT : DD/MM/RRRR-----
    NOTE:
    I tried :
    TARIH CHAR date_format DATE mask "DD/MM/RRRR" , same error.
    Edited by: 904386 on Jan 24, 2012 6:42 AM

    I have no experience with external tables but my understanding is that they are described the same was as you would describe a file to be loaded by SQL Loader. When ever I create a sql loader control file to load a date column I use syntax like this:
    DATE_M                                             "TO_DATE(:DATE_M,'dd.mm.yyyy')",Not sure if that will work for external tables or not.

  • Cannot import Excel dates

    I am using Oracle SQL Developer v1.2.1 and I'm trying to import an Excel file that contains date values formatted as 21-Sep-2007 11:29:45. During the import process, Data Preview shows that the date columns have values such as 39282.8. I have tried setting the Format on the Data Types tab and nothing is working. When I try import the file the following error messages are being displayed "Error at line 1:insert into EMMA_DB_PUSH_REPORT (MNTC_EVENT_ID,TKT_NBR,SCHED_COND_CD,NOTIFY_INDCR_CD,EVENT_DESC,OPEN_DT,START_DT,STOP_DT,SUBMIT_DT) VALUES(11848740186780000000000,3890.1,'SCHED','Y','APPROVED A SECOND TIME TO RE-GENERATE E-MAIL NOTIFICATION TO TSA SCHEDULED MAINTENANCE',TO_DATE('39282.8','DD-MON-YYYY HH:MI:SS'),TO_DATE('39347.6','DD-MON-YYYY HH:MI:SS'),TO_DATE('39348','DD-MON-YYYY HH:MI:SS'),TO_DATE('39346.4789930556','DD-MON-YYYY HH:MI:SS'))...SQL Error:ORA-01847: day of month must be between 1 and last day of month". Any suggestions would be greatly appreciated.

    Barry - The only way that I could import a date is if it was formatted as MM/DD/YYYY in Excel.

  • Problem in PlSql Block

    Hello All,
    when i am executing the below block then i m getttin the
    err ERR :ORA-01847: day of month must be between 1 and last day of month
    I am struggling to get where is the exact err ..
    Any one can suggest me any solution
    Emp Table
    EmpId Varchar2(20);
    Join_date Varchar2(8);
    DECLARE
    CURSOR C1 IS
    SELECT EMP_ID,Join_date FROM Emp;
    vJoin_dt DATE;
    vEmp Varchar2(20);
    vDate Varchar2(8);
    BEGIN
    OPEN C1;
    LOOP
    FETCH C1 INTO vEmp,vDate;
    EXIT WHEN C1%NOTFOUND;
    vJoin_dt:=to_date(NVL(vDate,SYSDATE),'ddmmyyyy');
    END LOOP;
    EXCEPTION
    WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('ERR :'||SUBSTR(SQLERRM,0,255));
    DBMS_OUTPUT.PUT_LINE('vEmp :'||Emp||' '||'vDate :'||vDate);
    END;
    ERR :ORA-01847: day of month must be between 1 and last day of month
    Thnks in advance

    I din't understand your requirement?
    Anyway! Is it you are looking for?
    SQL> SELECT * FROM EMPX;
    EMPID                JOIN_DAT
    A100                 05062006
    A101                 06062006
    SQL> DECLARE
      2  CURSOR C1 IS
      3  SELECT EMPID,Join_date FROM Empx;
      4  vJoin_dt DATE;
      5  vEmp Varchar2(20);
      6  vDate Varchar2(8);
      7  BEGIN
      8  OPEN C1;
      9  LOOP
    10  FETCH C1 INTO vEmp,vDate;
    11  EXIT WHEN C1%NOTFOUND;
    12  vJoin_dt:=to_date(NVL(vDate,SYSDATE),'ddmmyyyy');
    13  DBMS_OUTPUT.PUT_LINE(VJOIN_DT);
    14  END LOOP;
    15  EXCEPTION
    16  WHEN OTHERS THEN
    17  DBMS_OUTPUT.PUT_LINE('ERR :'||SUBSTR(SQLERRM,0,255));
    18  DBMS_OUTPUT.PUT_LINE('vEmp :'||VEMP||' '||'vDate :'||vDate);
    19  END;
    20  /
    05-JUN-06
    06-JUN-06
    PL/SQL procedure successfully completed.
    Please post your table's data, i guess it has two different date formats in join_Date"Message was edited by:
    Jameel

Maybe you are looking for