如果第一列为空,请选择另一列

时间:2015-04-16 13:20:26

标签: sql sql-server case

如果第一列为NULL,我只是找到一个例子,但如果它是空的,我需要另一列。

例如

SELECT artnr, arttxt, weightA if weight a is NULL or empty "" then weightB AS Weight
FROM artikel

5 个答案:

答案 0 :(得分:3)

您应该使用 CASE 表达式。

SELECT artnr, arttxt, 
       CASE WHEN weightA is NULL OR weightA = '' THEN weightB ELSE weightA END AS Weight
FROM artikel

答案 1 :(得分:1)

SELECT artnr, arttxt, 

CASE ISNULL(weightA, '') WHEN '' THEN weightB ELSE weightA END AS Weight

FROM artikel

答案 2 :(得分:1)

SELECT artnr,arttxt,        COALESCE(NULLIF(weightA,''),weightB)为[重量] 来自artikel

只是一个虚拟示例,用于显示它如何处理NULL和''空格

declare @t table (code varchar(10),food varchar(10))
insert into @t (code,food) values (NULL,'Sushi'),('','Rash')
select COALESCE(NULLIF(code,''),food) from @t

答案 3 :(得分:1)

 SELECT artnr, arttxt, COALESCE(NULLIF(weightA, ''), weightB) AS Weight
 FROM artikel

答案 4 :(得分:1)

没有案例陈述的解决方案

SELECT artnr
      ,arttxt
      ,COALESCE(NULLIF(weightA,''), weightB) 
FROM TableName