drop table test create table test (d varchar(30)) insert into test values('2015-02-31') insert into test values('2015-02-28')select * from test select cast(d as datetime) from test where isdate(d)=1/* d ------------------------------ 2015-02-31 2015-02-28(2 row(s) affected) ----------------------- 2015-02-28 00:00:00.000 */
嗯,表内确实找到了一些非日期格式的数据;可是我在测试的时候用order by 加top 1 固定在了一条记录上,该行的字段值为NULL,这样也会有影响吗?
而且我刚才试验了一下,如果在where条件内指定了查询该行,就不会报错了。
还有另外一个字段类型是char(8),记录的内容为员工的生日,里面的值都是日期格式和NULL。
也同样出现了这个问题
create table test (d varchar(30))
insert into test values('2015-02-31')
insert into test values('2015-02-28')select * from test
select cast(d as datetime) from test where isdate(d)=1/*
d
------------------------------
2015-02-31
2015-02-28(2 row(s) affected)
-----------------------
2015-02-28 00:00:00.000
*/
查出一个问题,是因为datetime的范围只到1753年,而数据内有小于1753年的数据,所以会造成超出范围的错误。
另一个原因是该字段有脏数据,非日期格式的值。非常感谢两位的帮助!