SQL常用函数

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

对字段出现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)

 

蓝色石头运营数据分析、网站建设、软件工具推荐编辑,欢迎投稿!
文章已创建 473

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部