SQL 如何在oracle中将YYYYMMDD转换为DD-Mon-YYYY?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29263586/
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
how to convert YYYYMMDD to DD-Mon-YYYY in oracle?
提问by Avinash Ganesh
I am trying to convert the date from YYYYMMDDto DD-Mon-YYYYin Oracle, but to_char
or to_Date
is not working. Can you please advise?
我正在尝试在 Oracle中将日期从YYYYMMDD转换为DD-Mon-YYYY,但to_char
或to_Date
无法正常工作。你能给些建议么?
select to_date(20150324,'DD-Mon-YY') from dual;
select to_char(20150324,'DD-Mon-YY') from dual;
select to_date(20150324,'DD-Mon-YY') from dual;
select to_char(20150324,'DD-Mon-YY') from dual;
I get an error message saying: - ORA-01861: literal does not match format string
我收到一条错误消息说: - ORA-01861: literal does not match format string
回答by Trinimon
Use this combination of to_char
and to_date
:
利用这种组合to_char
和to_date
:
select to_char (to_date('20150324','YYYYMMDD'), 'DD-Mon-YY') from dual;
Your mistake was, that you used the wrong date pattern. Additionally it's recommended to add''
, though it worked without them in this case.
您的错误是,您使用了错误的日期模式。此外,建议添加''
,尽管在这种情况下它可以在没有它们的情况下工作。
Check this Fiddle.
检查这个 Fiddle。