且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

日期转换问题。:yyyy / MM / dd到dd / MM / yyyy

更新时间:2023-02-26 13:48:27

尝试

SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]

源链接

附录1:

如我的评论所述,您需要先将字段转换为 DATETIME 类型,然后上述方法才能起作用。此示例应在您的SQL Server上运行:

As described in my comment, you need to convert your field to a DATETIME type before the above will work. This example should work on your SQL Server:

SELECT CONVERT(VARCHAR(10), CAST('2010-04-14' AS DATETIME), 103) AS [DD/MM/YYYY]

如果出现此异常:


消息242,级别16,状态3,第1行
将varchar数据类型转换为datetime数据类型导致

Msg 242, Level 16, State 3, Line 1 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

然后您需要将日期字段更改为 YYYY- MM-DD 格式,例如:2010-14-04

Then you need to change your date field to be in YYYY-MM-DD Format, EG: 2010-14-04

OR

在select语句之前添加以下行:

Add the following line before the select statement:

SET DATEFORMAT MDY -- Input dates are in the MM/DD/YYYY format, change to DMY to handle UK dates

示例:

SET DATEFORMAT MDY;

SELECT CONVERT(VARCHAR(10), CAST('2010-04-14' AS DATETIME), 103) AS [DD/MM/YYYY]