Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Simple? query question

Author  Topic 

darms21
Yak Posting Veteran

54 Posts

Posted - 2012-06-14 : 16:07:46
I have the following query

select *
from A
where name like '1' or name like '2' or name like '3'


The table has name like 1 and name like 2 but I want the results set to show as follows:


1 yes
2 yes
3 no



Any idea how I can get a result set that will include all items from the where clause in the result set and then indicate if it was found or not in the defined table?

Thanks!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-06-14 : 16:16:42
SELECT N.N, CASE WHEN A.Name IS NULL THEN 'no' ELSE 'yes' END
FROM (SELECT '1' N UNION ALL SELECT '2' UNION ALL SELECT '3') N
LEFT JOIN A ON N.N=A.Name
Go to Top of Page
   

- Advertisement -