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 Where Condition

Consider having the ff. table (#temp_table)

id column1 column2
1 value1 mark
2 value2 jim
3 value3 taylor
4 value4 tess
5 value5 mark
6 value6 jim

How do I make a select query where first, I will exclude all rows where column2 = mark
then exclude all rows where column2 = jim and column1 = value2

The expected result set should be

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

id column1 column2
3 value3 taylor
4 value4 tess
6 value6 jim

The query I have tried so far is

select * from #temp_table
where column2 != 'mark'
    and (column2 !='jim' and column1 != 'value2')

The result of my query

id column1 column2
3 value3 taylor
4 value4 tess

>Solution :

You can "negate" a group of conditions using the operator NOT before the group

select * from #temp_table
    where column2 != 'mark'
        and NOT (column2 ='jim' and column1 = 'value2')

More at: https://www.w3schools.com/sql/sql_and_or.asp

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