uba2012 uba2012 - 2 months ago 6
SQL Question

Join columns if one of them is null

I have 3 columns:

col1, int
col2, char(10)
col3, char(11)


only one of them has any value in them, and it can be either one of them.

i want to check which columns are null and add the one, whcih has a value to a fourth column: col4.

i use T-SQL

Answer
SELECT COALESCE(CONVERT(CHAR(11), col1), CONVERT(CHAR(11), col2), col3)
FROM yourtable;

TSQL: COALESCE