如何将列中存储为 nvarchar 的各种日期格式与 SQL 服务器中的 getdate() 进行比较
How to compare various date formats stored as nvarchar in a column with the getdate() in SQL Server
我正在使用 SQL Server 2008 R2
检查 table 中的值时,我发现很少有值存储为 2015-03-20T06:06:46
,也很少有值存储为 11/25/2014
。那么现在如何将 where 子句中的这两个值与 getdate()
进行比较
SELECT B.Value
FROM table1 A WITH (NOLOCK)
INNER JOIN table1 B WITH (NOLOCK) ON A.id = B.id
WHERE
A.Name = 'COMPLETED_AT'
AND CONVERT(smalldatetime, A.Value) < GETDATE() - 30
AND B.Name = 'RESULT'
收到错误消息
Conversion failed when converting character string to smalldatetime data type
执行上述查询时
示例table结构
ID Name Value
1 Result R12344
1 Completed_At 2015-03-20T06:06:46
2 Result R23445
2 Completed_At 2014-03-20T06:06:46
3 Result R83261
3 Completed_At 11/25/2014
列值属于 nvarchar(400)
数据类型
查询结果应显示录入超过30天的结果名称类型的值。
期待您的来信。
经过 link 后终于自己得到了我的查询的答案
https://msdn.microsoft.com/en-us/library/ms187928.aspx
CONVERT(nvarchar(400),A.value,126) < CONVERT(nvarchar(400),GETDATE()-30,126)
当我在 where 子句中使用上述值时,我没有得到任何错误,而且结果也是正确的
我正在使用 SQL Server 2008 R2
检查 table 中的值时,我发现很少有值存储为 2015-03-20T06:06:46
,也很少有值存储为 11/25/2014
。那么现在如何将 where 子句中的这两个值与 getdate()
SELECT B.Value
FROM table1 A WITH (NOLOCK)
INNER JOIN table1 B WITH (NOLOCK) ON A.id = B.id
WHERE
A.Name = 'COMPLETED_AT'
AND CONVERT(smalldatetime, A.Value) < GETDATE() - 30
AND B.Name = 'RESULT'
收到错误消息
Conversion failed when converting character string to smalldatetime data type
执行上述查询时
示例table结构
ID Name Value
1 Result R12344
1 Completed_At 2015-03-20T06:06:46
2 Result R23445
2 Completed_At 2014-03-20T06:06:46
3 Result R83261
3 Completed_At 11/25/2014
列值属于 nvarchar(400)
数据类型
查询结果应显示录入超过30天的结果名称类型的值。
期待您的来信。
经过 link 后终于自己得到了我的查询的答案 https://msdn.microsoft.com/en-us/library/ms187928.aspx
CONVERT(nvarchar(400),A.value,126) < CONVERT(nvarchar(400),GETDATE()-30,126)
当我在 where 子句中使用上述值时,我没有得到任何错误,而且结果也是正确的