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 QUERY CLAUS WHERE

I have this where clause

WHERE        (ACCDAT_0 >= '2020-01-01 00:00:00.000')and STA_0='3'
and
(NUM_0 LIKE '%FTC%') OR (NUM_0 LIKE '%NCC%') OR (NUM_0 LIKE '%DCF%')

The point is that if STA_0='1' is appearing in the query because its like NCC it possible to appear all NCC except the ones where the flag is 1?

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

>Solution :

This is really a comment, but it’s too large to fit in the comment space… elaboration on my comment above.

"OR" is a low precedence operator, meaning it is evaluated after all "AND." What this means is your query is doing this:

WHERE
 (ACCDAT_0 >= '2020-01-01 00:00:00.000' and
  STA_0='3' and
  NUM_0 LIKE '%FTC%') OR NUM_0 LIKE '%NCC%' OR NUM_0 LIKE '%DCF%'
  

Meaning if NUM_0 is like NCC or DCF, then the first two criteria are ignored. That may not be what you meant, but that’s what you said.

You might have meant this:

WHERE
  ACCDAT_0 >= '2020-01-01 00:00:00.000' and
  STA_0='3' and
 (NUM_0 LIKE '%FTC%' OR NUM_0 LIKE '%NCC%' OR NUM_0 LIKE '%DCF%')

And the larger lesson is… if you mix AND and OR, be sure to use parentheses so it does exactly what you mean.

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