简体   繁体   English

在 Oracle SQL 中将日期转换为特定格式

[英]Convert Date to specific format in Oracle SQL

I would like to convert the date to this format "2020-08-19" in Oracle SQL.我想在 Oracle SQL 中将日期转换为这种格式“2020-08-19”。

I did try doing this but it returns as 19-AUG-20我确实尝试过这样做,但它返回为 19-AUG-20

SELECT TO_DATE('2020-08-19', 'YYYY-MM-DD') FROM DUAL;

OUTPUT输出

19-AUG-20

I want the output in the below format:我想要以下格式的输出:

2020-08-19

Could anyone please help me with this?任何人都可以帮我解决这个问题吗? Thanks in advance!提前致谢!

If you have a date that you want to display as a formatted string , then use TO_CHAR() :如果您想将date显示为格式化字符串,请使用TO_CHAR()

SELECT TO_CHAR(DATE '2020-08-19', 'YYYY-MM-DD') FROM DUAL;

In the function, DATE '2020-08-19' is a literal date : that's a date datatype.在函数中, DATE '2020-08-19'文字日期:这是date数据类型。

As for your original code:至于你的原始代码:

SELECT TO_DATE('2020-08-19', 'YYYY-MM-DD') FROM DUAL;

What it does is interpret the input string as a date , given the format specified as the second argument.它所做的是将输入字符串解释为date ,给定指定为第二个参数的格式。 This returns a date , that is then displayed according to the NLS_DATE_FORMAT parameter of your database or session.这将返回一个date ,然后根据您的数据库或会话的NLS_DATE_FORMAT参数显示date You can change the parameter value at session level like so:您可以在会话级别更改参数值,如下所示:

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';

Now running the same query gives you the result that you want.现在运行相同的查询会给你你想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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