Select Column, If Blank Select from Another

Select column, if blank select from another

How about combining COALESCE and NULLIF.

SELECT COALESCE(NULLIF(SomeColumn,''), ReplacementColumn)
FROM SomeTable

Sql: select another field if one field is blank or 0

Use CASE.

SELECT CASE WHEN (special_selling_price IS NULL OR special_selling_price = 0)
THEN selling_price
ELSE special_selling_price
END AS SellPrice
FROM TableName

Check if a field is empty and get value from another column

You can use conditional IF to check for empty stings

SELECT ti.*,
tt.text_en,
IF(tt.text_es = '', tt.text_en,tt.text_es) as text_es,
IF(tt.text_pt = '', tt.text_en,tt.text_pt) as text_pt,
IF(tt.text_fr = '', tt.text_en,tt.text_fr) as text_fr
FROM table1_items ti JOIN
table_translations tt
ON tt.name = ti.name AND tt.cat = ti.cat;

Select a column if other column is null

You need the ISNULL function.

SELECT ISNULL(a, b)

b gets selected if a is null.

Also, you can use the WHEN/THEN select option, lookup in BOL. Essentially: its c switch/case block meets SQL.

How to select value from second column if first column is blank/null in SQL (MS SQL)?

If you think you have empty strings you can try :

select 
case when coalesce(PRICE1, '') <> '' then PRICE1
when coalesce(PRICE2, '') <> '' then PRICE2
when coalesce(PRICE3, '') <> '' then PRICE3
end AS PRICE
FROM A


Related Topics



Leave a reply



Submit