SQL conditional clause where

I store logos in the varbinary (max) field. I am looking for an SQL statement that says:

select logo where customerid=5

but if the logo for a particular client is NULL, then

select logo where customerid=18

which is the default logo. I would like to do this in one request, if possible. It can be done? Thanks.

+4
source share
2 answers
 SELECT COALESCE(b.logo, dflt.logo) AS logo FROM mytable dflt LEFT OUTER JOIN mytable b ON b.customerid=5 WHERE dflt.customerid=18 
+4
source
 SELECT TOP 1 logo FROM [Table] WHERE logo is not null and customerid IN (@CustomerID, 18) ORDER BY CASE WHEN customerid= 18 THEN 1 ELSE 0 END 

Notice how I processed the order. I suspect you might have clients with id <18 and some clients with id> 18, and therefore you need to be careful here.

+2
source

All Articles