tsql - show bool if row exists or doesn't -


i have table [contracts] columns [id], [number]. , have numbers in string format: '12342', '23252', '1256532'. want output this.

1535325 | no 12342   | yes 23252   | yes 434574  | no 1256532 | yes 

of course can write , rows have, how can determine if row doesn't exist , output above:

select [id]       ,[number]   [contracts]   [number] in    ('12342', '23252', '1256532') 

you can put values temporary table or table variable , left join:

declare @d table (number varchar(10)) insert @d values  ('12342'), ('23252'), ('1256532'), ('xxxx') -- last 1 not in contracts  select c.[id], c.[number], case when d.number null 'no' else 'yes' end [this number c in d also] [contracts] c     left join @d d on d.number = c.number 

for "opposite" use right join

select c.[id], d.[number], case when c.number null 'no' else 'yes' end [this number d in c also] [contracts] c     right join @d d on d.number = c.number 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -