简体   繁体   中英

Oracle SQL can not convert string date to date day

I need to convert the following date but I'm getting error:

ORA-01841: (full) year must be between -4713 and +9999, and not be 0

And the SQL:

SELECT TO_CHAR(TO_DATE('December 25th, 2004', 'MONTH DD, YYYY'), 'DD-MM-YY') FROM dual;

I think I'm getting error because of the string day format: 25th

If I remove the 'th' from the string it's working but I need to convert with it.

To take care of all such endings use replace 4 times:

with q as (
        select 'December 25th, 2004' dt from dual
        union
        select 'August 1st, 2004'  dt from dual
        union
        select 'December 2nd, 2004' dt from dual
        union
        select 'December 3rd, 2004' dt from dual
       )
select to_char( to_date(replace(replace(replace(replace(dt, 
                                   'th,', ''), 
                                   'st,', ''), 
                                   'nd,',''),
                                   'rd,',''),
                                    'MONTH DD YYYY'), 'DD-MM-YY') from q

If you enclose it in double quotes, you can add an arbitrary literal to your format mask.

SQL> ed
Wrote file afiedt.buf

  1  SELECT TO_CHAR(TO_DATE('December 25th, 2004', 'MONTH DD"th", YYYY'),
  2                 'DD-MM-YY')
  3*   FROM dual
SQL> /

TO_CHAR(
--------
25-12-04

Of course, this only works if the string always contains the literal string th . If you have other strings that have other suffixes (ie December 1st, 2004 ), you'll get an error

SQL> ed
Wrote file afiedt.buf

  1  SELECT TO_CHAR(TO_DATE('December 1st, 2004', 'MONTH DD"th", YYYY'),
  2                 'DD-MM-YY')
  3*   FROM dual
SQL> /
SELECT TO_CHAR(TO_DATE('December 1st, 2004', 'MONTH DD"th", YYYY'),
                       *
ERROR at line 1:
ORA-01861: literal does not match format string

If you want to handle both, you probably need to parse the original string to remove the suffix before converting the string to a date and then back to a different string

SQL> ed
Wrote file afiedt.buf

  1  WITH x AS (
  2    SELECT 'December 1st, 2004' str FROM dual UNION ALL
  3    SELECT 'December 25th, 2004' FROM dual
  4  )
  5  SELECT TO_CHAR(
  6           TO_DATE( SUBSTR( str, 1, INSTR( str, ',' ) - 3 ) ||
  7                      SUBSTR( str, INSTR( str, ',' ) ),
  8                    'MONTH DD, YYYY' ),
  9           'DD-MM-YY' )
 10*   FROM x
SQL> /

TO_CHAR(
--------
01-12-04
25-12-04

It would work without th , st and so on, ie:

SELECT TO_CHAR(TO_DATE(
       REGEXP_REPLACE('December 25th, 2004',  
                      '([[:digit:]]{1,2})(st|nd|rd|th)', '\1'), 
                      'MONTH DD, YYYY'), 'DD-MM-YY')
  FROM dual;

So this expression removes any one or two digits combination concatenated with st , nd , rd or th and calculates the date from it.

Hope this helps ... Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM