I want to check for data but ignore it if it's null or empty. Currently the query is as follows...
Select
Coalesce(listing.OfferText, company.OfferText, '') As Offer_Text,
from tbl_directorylisting listing
Inner Join tbl_companymaster company
On listing.company_id= company.company_id
But I want to get company.OfferText if listing.Offertext is an empty string, as well as if it's null.
What's the best performing solution?
This caters for spaces as well.
In this example, if
listing.OfferText
is NULL, the LEN() function should also return NULL, but that's still not > 0.Update
I've learned some things in the 5 1/2 years since posting this, and do it much differently now:
This is similar to the accepted answer, but it also has a fallback in case
Company.OfferText
is also null. None of the other current answers usingNULLIF()
also do this.Here is another solution:
This simple combination of COALESCE and NULLIF should do the trick:
Note: Add another empty string as the last COALESCE argument if you want the statement to return an empty string instead of NULL if both values are NULL.