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

Sql 如果第一列为空,请选择另一列,sql,sql-server,case,Sql,Sql Server,Case,我只是找到一个例子,如果第一列为NULL,但如果它为空,我需要取另一列 比如说 SELECT artnr, arttxt, weightA if weight a is NULL or empty "" then weightB AS Weight FROM artikel 你应该使用表达 SELECT artnr, arttxt, CASE WHEN weightA is NULL OR weightA = '' THEN weightB ELSE weightA END A

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

比如说

SELECT artnr, arttxt, weightA if weight a is NULL or empty "" then weightB AS Weight
FROM artikel
你应该使用表达

SELECT artnr, arttxt, 
       CASE WHEN weightA is NULL OR weightA = '' THEN weightB ELSE weightA END AS Weight
FROM artikel
选择artnr、ARTXT、, 聚结nullifweighta,,weightBas[重量] 来自阿蒂克尔

这只是一个虚拟示例,演示它如何处理NULL和空格


没有案例陈述的解决方案

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

查找大小写表达式,它正是您在此处需要的。@user2210516检查答案,不要忘记将其作为正确答案接受。这是错误的,只有当weightA为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
 SELECT artnr, arttxt, COALESCE(NULLIF(weightA, ''), weightB) AS Weight
 FROM artikel
SELECT artnr
      ,arttxt
      ,COALESCE(NULLIF(weightA,''), weightB) 
FROM TableName