SQL 在 Oracle 中将字符串日期转换为日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7920637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a string date into datetime in Oracle
提问by arsenal
How can I convert this string date to datetime in oracle.
如何在 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"')
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
回答by Forgotten Semicolon
Try this:
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
尝试这个:
TO_DATE('2011-07-28T23:54:14Z', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
回答by Sebastian Biner
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
嘿我有同样的问题。我试图将 '2017-02-20 12:15:32' varchar 转换为日期,TO_DATE('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS')
我收到的只是 2017-02-20 时间消失了
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.
我的解决方案是使用TO_TIMESTAMP('2017-02-20 12:15:32','YYYY-MM-DD HH24:MI:SS')
现在时间不会消失。
回答by Denisjc
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;