在 SQL SELECT 语句中更改日期格式(DD/MM/YYYY)

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

Change Date Format(DD/MM/YYYY) in SQL SELECT Statement

sqlsql-servertsql

提问by Fabre

The Original SQL Statement is:

原始 SQL 语句是:

SELECT SA.[RequestStartDate] as 'Service Start Date', 
       SA.[RequestEndDate] as 'Service End Date', 
FROM
(......)SA
WHERE......

The output date format is YYYY/MM/DD, but I want the output date format is DD/MM/YYYY. How can I modify in this statement?

输出日期格式为 YYYY/MM/DD,但我希望输出日期格式为 DD/MM/YYYY。如何修改此声明?

回答by AmanKumar

Try like this...

试试这样...

select CONVERT (varchar(10), getdate(), 103) AS [DD/MM/YYYY]

For more info : http://www.sql-server-helper.com/tips/date-formats.aspx

欲了解更多信息:http: //www.sql-server-helper.com/tips/date-formats.aspx

回答by PSo

Changed to:

变成:

SELECT FORMAT(SA.[RequestStartDate],'dd/MM/yyyy') as 'Service Start Date', SA.[RequestEndDate] as 'Service End Date', FROM (......)SA WHERE......

Have no idea which SQL engine you are using, for other SQL engine, CONVERT can be used in SELECT statement to change the format in the form you needed.

不知道您使用的是哪个SQL引擎,对于其他SQL引擎,可以在SELECT语句中使用CONVERT以您需要的形式更改格式。

回答by S K

There's also another way to do this-

还有另一种方法可以做到这一点-

select TO_CHAR(SA.[RequestStartDate] , 'DD/MM/YYYY') as RequestStartDate from ... ;

回答by JammoD

You will want to use a CONVERT() statement.

您将需要使用 CONVERT() 语句。

Try the following;

尝试以下操作;

SELECT CONVERT(VARCHAR(10), SA.[RequestStartDate], 103) as 'Service Start Date', CONVERT(VARCHAR(10), SA.[RequestEndDate], 103) as 'Service End Date', FROM (......) SA WHERE.....

See MSDN Cast and Convertfor more information.

有关详细信息,请参阅MSDN Cast and Convert

回答by Mohammad Anini

Try:

尝试:

SELECT convert(nvarchar(10), SA.[RequestStartDate], 103) as 'Service Start Date', 
       convert(nvarchar(10), SA.[RequestEndDate], 103) as 'Service End Date', 
FROM
(......)SA
WHERE......

Or:

或者:

SELECT format(SA.[RequestStartDate], 'dd/MM/yyyy') as 'Service Start Date', 
       format(SA.[RequestEndDate], 'dd/MM/yyyy') as 'Service End Date', 
FROM
(......)SA
WHERE......