sqlserver 中时间为空的处理小结


现将几种主要情况进行小结:
一、如何输入NULL值
如果不输入null值,当时间为空时,会默认写入"1900-01-01",在业务处理时很麻烦。
ctrl+0即可输入NULL值。
二、如何在sql语句中判断为NULL的时间字段
假设表为:TestTable
SN    DateTime1    DateTime2
1    2011-10-24   2011-10-25
2    NULL       2011-10-26
3    2011-10-25   NULL
用case进行查询,若写成:
select (case DateTime1 when NULL then 'a' else 'b' end) from TestTable
则查询结果为:

b

这显然不是想要的结果;需要写成:
select (case DateTime1 when DateTime1 then 'b' else 'a' end) from TestTable
其查询结果才为:

a

这才是想要的结果。
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3