急,SQL 日期查询的问题
数据库是SQL SERVER
从身份证号中提取出生日期,然后再根据要求时间做筛选。但不知道为什么,一直报错:从 nvarchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值
select * from (
SELECT CONVERT(datetime, SUBSTRING(shengfenNum, 7, 4) + '/' + SUBSTRING(shengfenNum, 11, 2) + '/' + SUBSTRING(shengfenNum, 13, 2),111) AS '出生日期'
FROM Relation
where
ISDATE( SUBSTRING(shengfenNum, 7, 4) + '/' + SUBSTRING(shengfenNum, 11, 2) + '/' + SUBSTRING(shengfenNum, 13, 2))>0
) as tab1
where 出生日期 < '2011-1-1 '
SELECT CONVERT(datetime, SUBSTRING(shengfenNum, 7, 4) + '/' + SUBSTRING(shengfenNum, 11, 2) + '/' + SUBSTRING(shengfenNum, 13, 2),111) AS '出生日期'
into #tab1
from Relation
where
ISDATE(SUBSTRING(shengfenNum,7,4)+'/'+SUBSTRING(shengfenNum,11,2)+'/'+SUBSTRING(shengfenNum,13,2))>0
select * from #tab1
where 出生日期 < '2011-1-1 '