把它的默认值去掉,在插入记录但不包含此列数据时,它就会为空. 如果想让已有此类数据的行该列为空,可以用 update tb set dt=null where dt='1900-01-01 00:00:00.000'
默认值去掉就应该为空了。修改语句是update tb set dt=null where dt='1900-01-01 00:00:00.000'
if object_id('tb','U') is not null drop table tb go create table tb ( id int primary key, name varchar(10), sr datetime ) go insert into tb select 1,'张三','' union all --空字符串就是'1900-01-01 00:00:00.000' select 2,'李四',null --设置为null,就不显示'1900-01-01 00:00:00.000'了select * from tb /* id name sr ----------- ---------- ----------------------- 1 张三 1900-01-01 00:00:00.000 2 李四 NULL(2 行受影响) */
如果想让已有此类数据的行该列为空,可以用
update tb set dt=null where dt='1900-01-01 00:00:00.000'
if object_id('tb','U') is not null
drop table tb
go
create table tb
(
id int primary key,
name varchar(10),
sr datetime
)
go
insert into tb
select 1,'张三','' union all --空字符串就是'1900-01-01 00:00:00.000'
select 2,'李四',null --设置为null,就不显示'1900-01-01 00:00:00.000'了select * from tb
/*
id name sr
----------- ---------- -----------------------
1 张三 1900-01-01 00:00:00.000
2 李四 NULL(2 行受影响)
*/
也就是说数据库已经是null的了,只是前端显示问题。
不能这样赋空set aa='' 这样的话系统会自动设置会默认值
应该这样赋值set aa=null
判断的时候也不要判断='',应该用isnull()或者 xx is null