If I type:
SELECT name FROM table WHERE name NOT IN ('Test1','Test2','Test3');
I can get the entries from the table that are not in the list. I want to do the opposite: Get the values from the list that are not in the table. For example, if table has a column named name that has the values 'Test1' and 'Test3' I want to compare that to ('Test1','Test2','Test3') and return Test2. Or as another example, if the table is empty, then return everything in the list: Test1, Test2, and Test3.
Is there a way to do this WITHOUT creating a new table with all of the values in the list?
Depending on how many values you have, you could do a few unions.
See: http://www.sqlfiddle.com/#!5/0e42f/1
select * from (
select 'Test 1' thename union
select 'Test 2' union
select 'Test 3'
)
where thename not in (select name from foo)
I usually use SELECT 'FOO' AS COL UNION SELECT 'BAR'
etc and then use the standard idiom of left joining and checking for NULL
to find missing elements.
CREATE TABLE #YourTable(
name nvarchar(50)
)
insert into #YourTable (name) values ('Test1'), ('Test3')
-- ALL
select * from #YourTable
--MISSING
select t1.* from (
select 'Test1' testName
union select 'Test2'
union select 'Test3') as t1
left outer join #YourTable yt on t1.testName = yt.name
where yt.name is null
DROP TABLE #YourTable
Gives output
name
--------------------------------------------------
Test1
Test3
(2 row(s) affected)
testName
--------
Test2
(1 row(s) affected)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With