SQL Server:datetime超出范围错误

我有这个问题

SELECT W.NUMBER,B.NAME,Br.NAME,W.WDATE,W.REPAIRSTATUS,W.REPAIRCOST,W.REMARK
FROM Work_Order AS W,Brands AS B,Branches AS Br
WHERE W.BRANDID = B.ID AND W.BRANCHID = Br.ID
AND CAST(WDATE as DATETIME)  < CAST('09/18/2012' AS DATETIME)

并且系统响应

The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.

也使用此查询它会给出相同的错误

SELECT W.NUMBER,B.NAME,Br.NAME,W.WDATE,W.REPAIRSTATUS,W.REPAIRCOST,W.REMARK
FROM Work_Order AS W,Brands AS B,Branches AS Br
WHERE W.BRANDID = B.ID AND W.BRANCHID = Br.ID
ORDER BY CAST(W.WDATE AS DATETIME)

WDATE就像’09 / 03/2012′,它是来自jquery ui datepicker的输入,带有mm / dd / yyyy日期格式

请帮帮我,我不知道如何解决它.

我正在研究SQL Server 2005.

编辑:我尝试与另一个SQL Server 2005相同的查询,它的工作正常,所以请如何修复此错误与SQL服务器设置?

最佳答案 我假设您的WDATE是一个varchar / char而不是应该的数据时间,您可以像这样投射它,但我建议您将数据类型更改为datetime.

试试这个:

SELECT W.NUMBER,B.NAME,Br.NAME,W.WDATE,W.REPAIRSTATUS,W.REPAIRCOST,W.REMARK 
FROM Work_Order AS W,Brands AS B,Branches AS Br 
WHERE W.BRANDID = B.ID AND W.BRANCHID = Br.ID 
AND CONVERT( DATETIME, WDATE, 101) < CONVERT( DATETIME, '09/18/2012', 101)

试试这个代码,它应该找到大多数无效日期

SELECT WDATE, W.NUMBER,B.NAME,Br.NAME,W.WDATE,W.REPAIRSTATUS,W.REPAIRCOST,W.REMARK 
FROM Work_Order AS W,Brands AS B,Branches AS Br 
WHERE W.BRANDID = B.ID AND W.BRANCHID = Br.ID 
AND WDATE not like '[0-1][0-9]/[0-3][0-9]/20[0-1][0-9]'
点赞