SQL脚本-对字段出现NULL值的处理以及COUNT

  • A+
所属分类:网站建设
摘要

select case when ‘字段名’ is null then ‘\N’ else convert(varchar(20),’字段名’) end as ‘NewName’

对字段出现NULL值的处理

--判断某些字段是否为空
--case
select case when '字段名' is null then '\N' else convert(varchar(20),'字段名') end as 'NewName'
select case when null is null then '\N' else convert(varchar(20),null) end as 'NewName'

--SQL Server 2005:coalesce
select coalesce('字符串类型字段','\N') as 'NewName'
select coalesce(convert(varchar(20),'非字符串类型字段'),'\N') as 'NewName'
select coalesce(convert(varchar(20),null),'\N') as 'NewName'

--coalesce,返回其参数中的第一个非空表达式
select Coalesce(null,null,1,2,null)union
select Coalesce(null,11,12,13,null)union
select Coalesce(111,112,113,114,null)

count的几种情况

--第一种
select count(*) from tablename

--第二种
select count(ID) from tablename

--第三种,1换成其它值也是可以的
select count(1) from tablename

/*
--第四种,这个不存在性能问题
idint 表ID(如果 indid = 0 或255)。否则为索引所属表的ID
Indid smallint 索引ID:
0=表
1=聚簇索引
>1=非聚簇索引
255=具有text或image数据的表条目。
rows int 基于indid=0 和 indid=1地数据级行数,该值对于indid>1重复。如果indid=255,rows设置为0。
当表没有聚簇索引时,Indid = 0 否则为 1。 
*/
select rows,indid from sysindexes where id = object_id('tablename') and indid in (0,1)

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: