SQL服务器:日期时间超出范围的错误(SQL Server: datetime out of ran

2019-09-22 16:06发布

我有这个疑问

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)

和系统响应

一个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
ORDER BY CAST(W.WDATE AS DATETIME)

WDATE是像'09 / 03 / 2012' ,它是从与毫米/日/年的日期格式jQuery UI的日期选择器的输入

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

我工作的SQL Server 2005上。

编辑:我试着用另一个SQL Server 2005相同的查询和它的正常工作,所以请如何解决这个错误与SQL Server的设置?

Answer 1:

我假设你WDATE是一个varchar / char和不DATATIME因为它应该是,你可以施放它这个样子,但是我推荐您更改数据类型为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]'


文章来源: SQL Server: datetime out of range error