Home select (a or b) as c from table;
Reply: 1

select (a or b) as c from table;

syg
1#
syg Published in 2018-01-11 23:17:26Z

Is there a way to select A if A column exists else select B column?

For a table

A    | B
-----|-----
foo  | bar
NULL | bar
foo  | NULL

Result would be

C
----
foo
bar
foo

It seems there's no function supporting this in docs.

Bacon Bits
2#
Bacon Bits Reply to 2018-01-11 23:38:30Z

It seems there's no function supporting this in docs.

Sure there is.

SELECT COALESCE(A,B) AS C
FROM aTable

SELECT ISNULL(A,B) AS C
FROM aTable

SELECT CASE WHEN A IS NOT NULL THEN A ELSE B END AS C
FROM aTable

Note that, in SQL Server, the query optimizer will rewrite COALESCE() into a CASE expression equivalent to the third example.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330506 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO