Reputation: 3452
I have an integration type project where my select column often has this code:
select
CASE MyColumn
WHEN '' THEN '&'
ELSE MyColumn
END
Is there a simpler approach? Sort of like coalesce with nulls? I'm finding the query really hard to read, etc.
Thanks
Upvotes: 2
Views: 7508
Reputation: 1434
This should work, and preserve nulls:
SELECT COALESCE(NULLIF(MyColumn, ''), NULLIF(MyColumn + '&', ''), MyColumn)
Upvotes: 0
Reputation: 124
Following on from @Michael Fredrickson's comment
NULLIF(COALESCE(NULLIF(COALESCE(MyColumn,'###'),''), '&'),'###')
Replace ### with anyvalue you know won't appear in the field. Not sure it's easier to read than the case statement though.
Upvotes: 0
Reputation: 69494
I guess you can try something like this...
COALESCE(NULLIF(MyColumn,''), '&')
Upvotes: 2