Manta
Manta

Reputation: 517

SQL Server query to select only values not in a list

As an example I have a table in SQL Server :

Id
1
3
5

and I have a list of values 1,2,3

What is the query to select values of my list not in the table.

Expected result :

Id
2

Upvotes: 1

Views: 41

Answers (2)

John Cappelletti
John Cappelletti

Reputation: 82010

Here are two approaches

Using Not Exists

Select *
 from  string_split('1,2,3',',') A
 Where not exists ( select 1 from YourTable where ID=Value )

Using a LEFT JOIN

Select A.Value
 from  string_split('1,2,3',',') A
 left Join  YourTable B on A.value=B.ID
 Where B.ID is null

Both Results are

Value
2

Upvotes: 1

Sukrit Anand
Sukrit Anand

Reputation: 11

SELECT * FROM id_table WHERE id NOT IN (1,2,3);

Upvotes: 0

Related Questions