2

I've got a table A with some data:

name
------
a
b
d

And I've got a list of names ('a', 'b', 'c', 'e') from which I would like to get those values that are not in the table. How can I do this? Which options do I have? This is not working (of course):

SELECT value FROM ('a', 'b', 'c', 'e') WHERE value NOT IN (SELECT name FROM A)

In this example the expected result would be:

value
------
c
e
robsch
  • 9,358
  • 9
  • 63
  • 104

2 Answers2

4

You can build a temp table with union and then join against it

select tmp.name
from
(
  select 'a' as name
  union all 
  select 'b'
  union all 
  select 'c'
  union all 
  select 'e'
) tmp
left join your_table t on t.name = tmp.name
where t.name is null

When you have more than just a few values I recommend putting those values into a real table and then join against it.

juergen d
  • 201,996
  • 37
  • 293
  • 362
0

Go to given below link you will get SPLIT function , just create this function it will be for future developement also. How to split string and insert values into table in SQL Server

After that Excecute the below query.

SELECT * 
into #temp
FROM [dbo].Split('a,b,c,e', ',')

select * from #temp where item not in (select * from A)

drop table #temp
Community
  • 1
  • 1
yatin parab
  • 174
  • 6