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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:28:01  来源:igfitidea点击:

how to convert YYYYMMDD to DD-Mon-YYYY in oracle?

sqloracleoracle11g

提问by Avinash Ganesh

I am trying to convert the date from YYYYMMDDto DD-Mon-YYYYin Oracle, but to_charor to_Dateis not working. Can you please advise?

我正在尝试在 Oracle中将日期从YYYYMMDD转换为DD-Mon-YYYY,但to_charto_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_charand to_date:

利用这种组合to_charto_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