Home » SQL & PL/SQL » SQL & PL/SQL » ORA-01847 problem (oracle 10g windows 7 pro)
ORA-01847 problem [message #637023] Thu, 07 May 2015 14:55 Go to next message
bobghw
Messages: 34
Registered: July 2012
Member
I have one field that is varchhar2 that holds a date as yyyymmdd "20150508".

I need to subtract this from a date field that holds a date as dd/mm/yyyy "01/05/2015"

Have tried various things but still get the ORA-01847 error msg.

select  to_date(mw_onset_date,'dd/mm/yyyy')as mw_onset_date,
        trunc(mws_last_update_date) as mws_last_update_date,
        trunc(mws_last_update_date)-to_date(mw_onset_date,'dd/mm/yyyy') as DaysBetween,
        pt_code
from
(
SELECT
    substr(mw.mw_onset_date,7,2)||'/'||substr(mw.mw_onset_date,5,2)||'/'||substr(mw.mw_onset_date,1,4) as mw_onset_date,
    trunc(mw.mws_last_update_date) as mws_last_update_date,
    c.pt_code
FROM
  K_MWREG mw, k_cpireg c  
WHERE
 c.pt_code = mw.pt_code
 and  mws_last_update_date is not null
 and substr(mw.mw_onset_date,5,2) <> 00
ORDER BY 
 mw.mw_onset_date desc
 )sub
 where mw_onset_date is not null
 and mw_onset_date > to_date('01/01/2009','dd/mm/2015')
Re: ORA-01847 problem [message #637027 is a reply to message #637023] Thu, 07 May 2015 15:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I have one field that is varchhar2 that holds a date as yyyymmdd "20150508".
>I need to subtract this from a date field that holds a date as dd/mm/yyyy "01/05/2015"

what datatype results when you subtract one date from another date?

what should be the actual results when you successfully subtract the two value above?
Re: ORA-01847 problem [message #637030 is a reply to message #637027] Thu, 07 May 2015 15:26 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Thanks for the reply

I just need to know how many days between dates if this is returned as numeric or char that is fine.
Re: ORA-01847 problem [message #637031 is a reply to message #637030] Thu, 07 May 2015 15:31 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
If you won't answer my questions, why should I answer your questions?
Re: ORA-01847 problem [message #637033 is a reply to message #637031] Thu, 07 May 2015 16:00 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
ok

According to the Oracle 11.2 SQL Reference, when you subtract 2 DATE datatypes, the result will be a NUMBER datatype

It should return the number of days as a number


Re: ORA-01847 problem [message #637034 is a reply to message #637033] Thu, 07 May 2015 16:02 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member

  1  DECLARE
  2  STR1 VARCHAR2(8) := '20150508';
  3  DATE1 DATE := TO_DATE('01/05/2015','DD/MM/YYYY');
  4  DATE2 DATE;
  5  NUM1 NUMBER;
  6  BEGIN
  7  DATE2 :=  TO_DATE(STR1,'YYYYMMDD');
  8  DBMS_OUTPUT.PUT_LINE(DATE2);
  9  NUM1  := DATE2 - DATE1;
 10  DBMS_OUTPUT.PUT_LINE(NUM1 );
 11* END;
SQL> /
08-MAY-15
7

PL/SQL procedure successfully completed.


Re: ORA-01847 problem [message #637036 is a reply to message #637034] Thu, 07 May 2015 16:16 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Thank you.
Re: ORA-01847 problem [message #637050 is a reply to message #637036] Fri, 08 May 2015 09:53 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Why a procedure

select to_date('20150508','yyyymmdd') - to_date('01/05/2015','dd/mm/yyyy') from dual;

Re: ORA-01847 problem [message #637106 is a reply to message #637050] Sun, 10 May 2015 15:47 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Thanks Bill I hadn't realized that I could subtract dates in different formats, but the underlying data is the same I suppose so yes.

I also discovered an issue in the way some of the data has been entered no month or day value in some cases so issues there.
but this query works for now until the data entry is corrected.

SELECT
     c.pt_code,
     to_date(mw.mw_onset_date,'yyyymmdd')as mw_onset_date,
     trunc(mw.mws_last_update_date) as mws_last_update_date ,
     trunc(mw.mws_last_update_date)- to_date(mw.mw_onset_date,'yyyymmdd')as DaysBetween     
FROM
     K_MWREG mw , k_cpireg c  
WHERE
     c.pt_code = mw.pt_code
     and mw.mws_last_update_date is not null
     and  mw.mw_onset_date not like '%00'
Re: ORA-01847 problem [message #637125 is a reply to message #637106] Mon, 11 May 2015 03:42 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Dates don't actually have formats. Or more precisely they all have the same internal format that isn't really human readable.
Have a read of this: http://edstevensdba.wordpress.com/2011/04/07/nls_date_format/
Re: ORA-01847 problem [message #637149 is a reply to message #637125] Mon, 11 May 2015 16:25 Go to previous message
bobghw
Messages: 34
Registered: July 2012
Member
Cheers CookieMonster that has clarified things.
Previous Topic: Finding positions of all 'X' in string?
Next Topic: Pivoting Data
Goto Forum:
  


Current Time: Fri Apr 26 19:46:05 CDT 2024