Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Expected result :

Id
2

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading