SQL TO_DATE 为不同的格式掩码返回不同的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13934111/
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 12:40:38  来源:igfitidea点击:

TO_DATE returning different dates for different format masks?

sqloracleto-date

提问by Kanagavelu Sugumar

I am having query on the below sql. Please help me.

我正在查询以下 sql。请帮我。

 select to_date(sysdate, 'DD MONTH YYYY') , to_date(sysdate, 'yyyy/mm/dd hh24:mi:ss')
 from dual where 
 to_date(sysdate, 'DD MONTH YYYY') < to_date(sysdate, 'yyyy/mm/dd');

1) to_date(sysdate, 'DD MONTH YYYY') this will give Date object without time (may be time is 00:00).
Am i correct ? If not how will i get only Date object without Time in it?
2) From the above query It seems to_date(sysdate, 'yyyy/mm/dd') is greater than to_date(sysdate, 'DD MONTH YYYY'). Why ??

1) to_date(sysdate, 'DD MONTH YYYY') 这将给出没有时间的 Date 对象(可能时间是 00:00)。
我对么 ?如果不是,我将如何只获得没有时间的 Date 对象?
2) 从上面的查询似乎 to_date(sysdate, 'yyyy/mm/dd') 大于 to_date(sysdate, 'DD MONTH YYYY')。为什么 ??



Update



更新



1) My aim in the above URL is to find out to_date function will only return date (or along with time) though format 'DD MONTH YYYY' is not mentioning the time chars(hh:mm..) in it.
2) From the response i got that to_date will return Date with time always though we didn't mention the time chars.
3) Finally to get only DATE we should use trunc() / to_date(to_char(sysdate,'mm-dd-yyyy'),'mm-dd-yyyy')

1)我在上面的 URL 中的目标是找出 to_date 函数只会返回日期(或连同时间),尽管格式 'DD MONTH YYYY' 没有提到其中的时间字符(hh:mm..)。
2)从我得到的响应中,尽管我们没有提到时间字符,但 to_date 将始终返回带时间的日期。
3) 最后只获得 DATE 我们应该使用 trunc() / to_date(to_char(sysdate,'mm-dd-yyyy'),'mm-dd-yyyy')

Check the below

检查以下

select to_char(trunc(sysdate), 'yyyy/mm/dd hh24:mi:ss'), 
to_char(to_date(to_char(sysdate, 'yyyy-MM-dd'),'yyyy/mm/dd hh24:mi:ss'),'yyyy/mm/dd hh24:mi:ss') 
from dual;

-->hh24 :24 is important to get 00:00 format.

--> hh24 :24 对于获取 00:00 格式很重要。

回答by DazzaL

you've just shot yourself in the foot. its called implicit datatype conversion.

你刚刚中了自己的脚。它称为隐式数据类型转换。

there is no TO_DATE(date)(which is what you're asking for) , so oracle has converted your query to TO_DATE(TO_CHAR(sysdate), 'DD MONTH YYYY')where TO_CHAR(sysdate)will pick up the default NLS format mask. depending on your default mask (see NLS_SESSION_PARAMETERS), your 1st may have resolved to the year 0012 and the 2nd to the year 0018. so 0012 is less than 0018.

没有TO_DATE(date)(这就是您要的),因此 oracle 已将您的查询转换为TO_DATE(TO_CHAR(sysdate), 'DD MONTH YYYY')whereTO_CHAR(sysdate)将选择默认的 NLS 格式掩码。根据您的默认掩码(请参阅 NLS_SESSION_PARAMETERS),您的第一个可能已解析为 0012 年,第二个可能已解析为 0018 年。因此 0012 小于 0018。

if you want to truncate the sysdate, then just do trunc(sysdate)

如果你想截断系统日期,那么就这样做 trunc(sysdate)

回答by khalidmehmoodawan

If you want to see the time part when selecting SYSDATE. You may set NLS_DATE_FORMAT before issuing a SELECT statement.

如果您想在选择 SYSDATE 时查看时间部分。您可以在发出 SELECT 语句之前设置 NLS_DATE_FORMAT。

ALTER SESSION SET NLS_DATE_FORMAT = 'yyyy-mm-dd hh24:mi:ss';

In IDEs (Today, PLSQL Developer, SQL Developer etc.) NLS_DATE_FORMATcan be set permanently to be used by every select statement you execute. In that case you dont need to run ALTER SESSIONcommand prior to SELECT.

在 IDE(今天,PLSQL Developer、SQL Developer 等)中,可以永久设置NLS_DATE_FORMAT以供您执行的每个选择语句使用。在这种情况下,您不需要在 SELECT 之前运行ALTER SESSION命令。

回答by Micha? Niklas

Ad. 1. In my test environment I got date with time:

广告。1. 在我的测试环境中,我得到了时间日期:

select to_date('15 Maj 1998', 'DD MONTH YYYY') from dual;

Result (for Polish settings):

结果(波兰语设置):

1998-05-15 00:00:00

Ad. 2. Why do you use sysdatewhen calling to_date? This function expects string describing datetime in given format. In my environment such call fails. You probably have other NLS settings so sysdatecan be cast to string and such string gives you strange result. Use string, not sysdate.

广告。2.sysdate打电话时为什么用to_date?此函数需要以给定格式描述日期时间的字符串。在我的环境中,此类调用失败。您可能有其他 NLS 设置,因此sysdate可以转换为字符串,而这样的字符串会给您带来奇怪的结果。使用字符串,而不是sysdate.