29

How can I convert this string date to datetime in oracle.

2011-07-28T23:54:14Z

Using this code throws an error:

TO_DATE('2011-07-28T23:54:14Z',  'YYYY-MM-DD HH24:MI:SS')

How can this be done?

Error report:
SQL Error: ORA-01861: literal does not match format string
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.

Update:-

TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')

I only see the date not time in the column

28-JUL-11
arsenal
  • 23,366
  • 85
  • 225
  • 331
  • 1
    Your format doesn't have T and Z letters, or am I wrong? Try to parse 2011-07-28 23:54:14, if it will pass, then use regex to normalize your input strings. – d1e Oct 27 '11 at 18:41
  • @p.campbell but in my case. String date is in different format with t and z so is there any way to convert that directly to datetime format. Or I have to parse it to make it more clearer then convert that to datetime format.. – arsenal Oct 27 '11 at 18:41

3 Answers3

60

Try this: TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')

Forgotten Semicolon
  • 13,909
  • 2
  • 51
  • 61
  • It is working fine.. But I only see the Date not time in the oracle table.. The result that I got is 28-JUL-11 not the exact way I wanted.. Why is it happening? And I declare the dataType as date. – arsenal Oct 27 '11 at 18:46
  • 8
    @RaihanJamal, that's just the default format for date. The time is actually stored in that field as well. You can see it using the TO_CHAR method. – Forgotten Semicolon Oct 27 '11 at 18:49
  • 2
    what's wrong with this sytax `TO_DATE('2011-10-25 21:48:01.585', 'YYYY-MM-DD HH24:MI:SS')` – arsenal Oct 27 '11 at 19:34
  • Looks like it has milliseconds on the time as well. If this format is in the same dataset as the item in the OP, you need to follow the previously given advice of standardizing your data. – Forgotten Semicolon Oct 27 '11 at 19:38
  • @lining, It should be `TO_DATE('2011-10-25 21:48:01.585', 'YYYY-MM-DD HH24:MI:SS.FF3')`, FFn may have n=1..9, depending on the number of decimal places you have. – Krzysztof Jędrzejewski Oct 18 '16 at 13:37
2

Hey I had the same problem. I tried to convert '2017-02-20 12:15:32' varchar to a date with TO_DATE('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS') and all I received was 2017-02-20 the time disappeared

My solution was to use TO_TIMESTAMP('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS') now the time doesn't disappear.

  • That is probably only because of the display formatting of your SQL client. An Oracle `DATE` may also include a time so `TO_DATE` will not necessarily drop the time portion. – Jon Heller Feb 21 '17 at 14:49
2

You can use a cast to char to see the date results

 select to_char(to_date('17-MAR-17 06.04.54','dd-MON-yy hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') from dual;
DenisJC
  • 43
  • 7