日期:2014-05-18  浏览次数:20437 次

一个表中有很多字段,可以为空,怎么得到第一个非空字段。
FIELD1 FIELD2 FIELD3 FIELD4 FIELD5
A 1 C 1 D1
  B2 C2
  C3


每个字段都可能为空 取得 一条记录中第一个非空的字段。

------解决方案--------------------
SQL code

create table tb(col1 int,col2 int,col3 int)
insert into tb
select null,null,1 union all
select null,2,1
go

select coalesce(col1,col2,col3) col
from tb

drop table tb


/************************

col
-----------
1
2

(2 行受影响)