Reputation: 683
I just find an example for this if the first column is NULL but i need to take another column if it's empty.
For example
SELECT artnr, arttxt, weightA if weight a is NULL or empty "" then weightB AS Weight
FROM artikel
Upvotes: 0
Views: 96
Reputation: 69524
A solution without the case statement
SELECT artnr
,arttxt
,COALESCE(NULLIF(weightA,''), weightB)
FROM TableName
Upvotes: 1
Reputation: 35780
SELECT artnr, arttxt, COALESCE(NULLIF(weightA, ''), weightB) AS Weight
FROM artikel
Upvotes: 1
Reputation: 8865
SELECT artnr, arttxt, COALESCE(NULLIF(weightA,''),weightB)as [Weight] FROM artikel
just an dummy example to show how it handles NULL and '' spaces
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
Upvotes: 1
Reputation: 9053
You should use CASE expression.
SELECT artnr, arttxt,
CASE WHEN weightA is NULL OR weightA = '' THEN weightB ELSE weightA END AS Weight
FROM artikel
Upvotes: 3
Reputation: 192
SELECT artnr, arttxt,
CASE ISNULL(weightA, '') WHEN '' THEN weightB ELSE weightA END AS Weight
FROM artikel
Upvotes: 1