I have a address in more than one column in a table.
SELECT FirstName, LastName, StreetAddress, City, Country, PostalCode
FROM Client
I am trying to concatenate address related columns into one filed using Comma (,) as a separator but if any of the column “eg. City” is null or empty, comma should not be there.
How to use ternary operator in TSQL like one has in c#?
Or suggest me the best practice?
Thanks
When you concatenate anything with a null, it returns null. So I’m trying to concatenate a comma with the given column value and if that expression returns null, I use Coalesce to return an empty string. At the end, if I get a value, the entire result will start with a comma. So I remove that comma using the Stuff function.
If you only want the address, then obviously you would only include those columns: