SELECT B.Value
FROM table1 A WITH (NOLOCK)
INNER JOIN table2 B WITH (NOLOCK) ON A.id = B.id
WHERE
A.Name = 'COMPLETED_AT'
AND CONVERT(smalldatetime, A.Value) < GETDATE() - 30
AND B.Name = 'RESULT'
收到错误消息
将字符串转换为smalldatetime数据类型时转换失败
执行上述查询时
示例表结构
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
列值为nvarchar(400)数据类型
查询结果应显示结果名称类型的值,这些值已被输入超过30天。
期待您的回复。
答案 0 :(得分:0)
它工作正常,只是将 Table2 更改为 Table1 ,因为您说是自我加入。
CREATE TABLE Table1
([ID] int, [Name] varchar(12), [Value] varchar(19))
;
INSERT INTO Table1
([ID], [Name], [Value])
VALUES
(1, 'Result', 'R12344'),
(1, 'Completed_At', '2015-03-20T06:06:46'),
(2, 'Result', 'R23445'),
(2, 'Completed_At', '2014-03-20T06:06:46')
;
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'
结果
Value
R23445
答案 1 :(得分:0)
在Datetime Coloumn字段&amp;中避免空修复此错误(将字符串转换为smalldatetime数据类型时转换失败。):
首先,我给这个类型得到这个错误:
select * from Tabel1 where ISNULL(Datecoloumn1,**'0'**)!='1900-01-01 00:00:00'
单引号为零是问题
我修好了这个方法:
select * from Tabel1 where ISNULL(Datecoloumn1,**0**) != '1900-01-01 00:00:00'