What is the most concise way to return CHAR(1) indicator Y/N if one of n columns in a row contain a non-null value?
Performance is important, but not the primary consideration, in this case.
The straightforward way seems to be:
SELECT CASE WHEN (C.TerminatedDate IS NULL
AND C.SelfClosedDate IS NULL
AND ...)
THEN 'Y'
ELSE 'N' END AS 'OpenInd'
FROM Customers C
Curious if there is a better way; aware of COALESCE():
SELECT CASE WHEN COALESCE (C.TerminatedDate, C.SelfClosedDate, ...) IS NULL
THEN 'Y'
ELSE 'N' END AS 'OpenInd'
FROM Customers C
Is there a better way?
Database server is SQL Server 2008.
Since no one is suggesting it, and the question asks for
concise..For the same data types, a straight COALESCE is best
If you will be dealing with different data types, try a modified COALESCE
Sample:
If you are not using exotic types like VARCHAR(MAX) or IMAGE, then you can use SQL_VARIANT with COALESCE