Select column, if blank select from another

graham.reeds picture graham.reeds · Nov 17, 2009 · Viewed 79.3k times · Source

How does one detect whether a field is blank (not null) and then select another field if it is?

What I really need is a IsBlank function that works the same as IsNull but with with blanks.

REPLACE doesn't work with blanks, COALESCE only works with NULLS.

Answer

PHeiberg picture PHeiberg · Nov 17, 2009

How about combining COALESCE and NULLIF.

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