日期转换问题。:yyyy / MM / dd到dd / MM / yyyy [英] Date conversion issue.: yyyy/MM/dd to dd/MM/yyyy
问题描述
我有一个日期列,其日期格式为
I have a date column where the date format is
2010-04-14
在SQL Server中。是否有任何可能的方式来检索日期格式为
in SQL Server. Is there any possible way to retrieve the date format as
14/04/2010
在选择语句中?
推荐答案
尝试
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]
这篇关于日期转换问题。:yyyy / MM / dd到dd / MM / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!