user2210516
user2210516

Reputation: 683

Select another column if the first one is empty

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

Answers (5)

M.Ali
M.Ali

Reputation: 69524

A solution without the case statement

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

Upvotes: 1

Giorgi Nakeuri
Giorgi Nakeuri

Reputation: 35780

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

Upvotes: 1

mohan111
mohan111

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

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

legui
legui

Reputation: 192

SELECT artnr, arttxt, 

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

FROM artikel

Upvotes: 1

Related Questions