SQL 如何查询日期格式YYYY-MM-dd HH-MM-ss?

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

How to query the date format YYYY-MM-dd HH-MM-ss?

sqloracle

提问by user3180160

How to query the date with the format :

如何使用格式查询日期:

02/13/2013 12:05:48

2013 年 2 月 13 日 12:05:48

select * 
from TOTAL_SUBS 
where TIME BETWEEN TO_DATE('2013-13-02 00:00:00', 'SYYYY-MM-DD HH24:MI:SS') 
AND TO_DATE('2013-12-14 00:00:00', 'SYYYY-MM-DD HH24:MI:SS');

I have try with that code but what i expected from the date only came out the 02/13/2013, There is no Time "12:05:48" came out in the result.

我已尝试使用该代码,但我从日期开始的预期仅在 2013 年 2 月 13 日出现,结果中没有时间“12:05:48”出现。

回答by Maheswaran Ravisankar

EDIT: It advisable to do to_char() to requirement format, as the NLS_DATE_FORMAT may not be same everywhere you run your Query!

编辑:建议对要求格式执行 to_char(),因为 NLS_DATE_FORMAT 在您运行查询的任何地方都可能不同!

If, at all you want to change NLS, please do this.

如果您想更改 NLS,请执行此操作。

alter session set nls_date_format='MM/DD/YYYY HH24:MI:SS';

Finally, a TO_CHAR() conversion in the select. Actually, when you want print(see) a DATE datatype in the format you expect, use this.

最后,选择中的 TO_CHAR() 转换。实际上,当您想要以您期望的格式打印(查看) DATE 数据类型时,请使用它。

select * , TO_CHAR(TIME,'MM/DD/YYYY HH24:MI:SS') my_datetime
from TOTAL_SUBS 
where TIME BETWEEN TO_DATE('2013-02-13 00:00:00', 'SYYYY-MM-DD HH24:MI:SS') 
AND TO_DATE('2013-12-14 23:59:59', 'SYYYY-MM-DD HH24:MI:SS');

Edited the month and the timestamp to include till mdnight. Hope this helps!

编辑月份和时间戳以包括直到 mdnight。希望这可以帮助!

回答by Hawk

What application do you use ?

你使用什么应用程序?

if Oracle SQL Developer, you just need to go to Tools menu -> Database -> NLS.

如果是 Oracle SQL Developer,则只需转到 Tools 菜单 -> Database -> NLS。

Add the date format as you wish.

根据需要添加日期格式。

SELECT * 
FROM   total_subs 
WHERE  time BETWEEN To_date('2013-02-13 00:00:00', 'YYYY-MM-DD HH24:MI:SS') AND 
                    To_date('2013-12-14 00:00:00', 'YYYY-MM-DD HH24:MI:SS'); 

Try your query again.

再次尝试您的查询。

Without this, no matter how the query structure is, it will never show the time

没有这个,无论查询结构如何,永远不会显示时间