我正在尝试按日期过滤一些记录,但日期时间字段似乎存储为 varchar,我正在努力转换它。以下是数据示例:
ID DateField
0002 14/04/1989 01:30
0003 16/04/1989 09:45
0004 16/04/1989 06:00
0005 19/04/1989 01:07
0006 21/04/1989 16:03
当我使用时
cast(Datefield as datetime)
我收到以下错误消息:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
编辑 - 这实际上确实以日期时间格式显示结果,但带有错误消息,这是为什么?
将日期字段转换为有效日期时间格式的最佳方法是什么?谢谢
最佳答案
将 CONVERT 与样式结合使用
CONVERT(DATETIME,Datefield ,103)
要查找不良数据,请查看返回的内容
set dateformat dmy
select Datefield from table where isdate(where)=0
您应该始终使用正确的 DATETIME 数据类型来存储日期时间值
关于sql - 将 varchar 转换为日期时间字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31961781/