SQL DD-MMM-YYYY 格式的日期,例如 29-JAN-2015

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

date in DD-MMM-YYYY format eg 29-JAN-2015

sqloracledate

提问by Sravanth Kothuri

I want the date in DD-MMM-YYYYformat eg 29-JAN-2015.

我想要DD-MMM-YYYY格式的日期,例如 29-JAN-2015。

I have tried with below query

我试过下面的查询

SELECT TRIM(TO_DATE('29 Jan 2015'
                ,'DD MON YY')) FROM DUAL

I got result as: 29-JAN-15

我得到的结果是:29-JAN-15

But I am expecting: 29-JAN-2015in date format not in char format

但我期待:29-JAN-2015日期格式而不是字符格式

采纳答案by Sravanth Kothuri

Thanks for answers. I got the solution. First we need to alter the session as below:

感谢您的回答。我得到了解决方案。首先,我们需要改变会话如下:

alter session set nls_date_format='DD-MON-YYYY';

更改会话集 nls_date_format='DD-MON-YYYY';

then run the query: SELECT TRIM(TO_DATE('29 Jan 2015' ,'DD MON YYYY')) FROM DUAL Now I got result as:29-JAN-2015

然后运行查询: SELECT TRIM(TO_DATE('29 Jan 2015' ,'DD MON YYYY')) FROM DUAL 现在我得到的结果为:29-JAN-2015

回答by rbm

Im assuming Oracle DB:

我假设 Oracle 数据库:

select to_char(SYSDATE, 'dd-Mon-yyyy') from dual

Returns

退货

29-Jan-2015

回答by Thorsten Kettner

What you are doing is take the string '29 Jan 2015' and make it a date using the format 'DD MON YY'. This should fail of course for '2015' not matching 'yy', but Oracle is lenient here.

您正在做的是获取字符串 '29 Jan 2015' 并使用格式 'DD MON YY' 将其设为日期。对于 '2015' 与 'yy' 不匹配,这当然应该失败,但 Oracle 在这里很宽容。

Then you use TRIM on the date. But TRIM is for strings. What happens is that you get shown '29 Jan 15'. I am getting shown '29.01.15' instead of the usual '29.01.2015'. However the behavior: Don't use TRIM on dates, its behavior is nowhere specified as far as I am aware. Use TO_CHAR to format a date in output.

然后在日期上使用 TRIM。但是 TRIM 是用于字符串的。发生的情况是您会看到“29 Jan 15”。我看到的是“29.01.15”而不是通常的“29.01.2015”。但是行为:不要在日期上使用TRIM,据我所知,它的行为没有任何规定。使用 TO_CHAR 格式化输出中的日期。

If you only select a date without TO_CHAR you get the date shown in some standard format, which can be '29 Jan 2015' or '29 Jan 15' or '29.01.2015' or '01/29/2015' depending on the app you are using and possibly some setting therin.

如果你只选择一个没有 TO_CHAR 的日期,你会得到以某种标准格式显示的日期,根据应用程序可以是“29 Jan 2015”或“29 Jan 15”或“29.01.2015”或“01/29/2015”你正在使用,可能还有一些设置。

For completeness sake:

为了完整起见:

TO_DATE takes a string ('29 Jan 2015' in your case) and converts it to a date. If the string contains names, make sure that you specify the appropriate language setting:

TO_DATE 接受一个字符串(在您的情况下为 '29 Jan 2015')并将其转换为日期。如果字符串包含名称,请确保指定适当的语言设置:

TO_DATE('29 Jan 2015', 'dd mon yyyy', ''NLS_DATE_LANGUAGE=AMERICAN')

To get a formatted string from a date, use TO_CHAR:

要从日期获取格式化字符串,请使用 TO_CHAR:

TO_CHAR(sysdate, 'dd MON yyyy', 'NLS_DATE_LANGUAGE=AMERICAN')

Usually you don't do that, however. You select a date as is and have your app (written in PHP, Java or whatever) care about how to display it appropriately to the user's computer's settings.

但是,通常您不会这样做。您按原样选择一个日期,并让您的应用程序(用 PHP、Java 或其他语言编写)关心如何根据用户计算机的设置适当地显示它。

回答by Mahesh

In SQL Server the query

在 SQL Server 中查询

select CONVERT(nvarchar, GETDATE(), 106) as [Converted Date]

returns:

返回:

29 Jan 2015

回答by gnuchu

Manu is correct. Oracle publish a full list of date format specifiers.

马努是对的。Oracle 发布了完整的日期格式说明符列表。

http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm#CDEHIFJA

http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm#CDEHIFJA

回答by Jay Vomex

Use CONVERT(VARCHAR(11),GETDATE(),106)

CONVERT(VARCHAR(11),GETDATE(),106)

See detailed explanation here: http://www.w3schools.com/sql/func_convert.asp

详细解释见这里:http: //www.w3schools.com/sql/func_convert.asp

回答by Manu

Can you try:

你能试一下吗:

SELECT TRIM(TO_DATE(SYSDATE ,'DD MON YYYY')) FROM DUAL

YYwill only show 2 ciphers, instead of YYYYthat will show 4.

YY只会显示 2 个密码,而不是YYYY显示 4 个。