zl程序教程

您现在的位置是:首页 >  后端

当前栏目

探讨:sql插入空,默认1900-01-0100:00:00.000的解决方法详解

方法SQL 详解 解决 默认 插入 探讨 01
2023-06-13 09:15:01 时间

sql2005若字段定义的类型为datetime,插入为""(空),那么会默认值为1900-01-0100:00:00.000

解决方法查询的时候过滤下cast(nullif("","")asdatetime)

复制代码代码如下:

selectcast(""asdatetime)
   ,cast(nullif("","")asdatetime)
   ,isnull(cast(nullif("","")asdatetime),getdate())
/*
---------------------------------------------------------------------
1900-01-0100:00:00.000NULL                   2009-02-2517:18:15.140

(1行受影响)

*/