PDA

View Full Version : [SOLVED:] SQL query using where with multiple criterias



Alternsti
01-26-2022, 11:23 AM
I'm able to connect and retreive data using the SQL string below BUT it seems the WHERE isn't working properly. hdr_RD section
http://icons.iconarchive.com/icons/double-j-design/ravenna-3d/24/File-Copy-icon.png
SQL = "SELECT [hdr11], [hdr31], [hdr32], [hdr33], [hdr34] FROM " & strTable & " WHERE [hdr_RD] Like 'New' OR [hdr_RD] Like 'Hold' AND [nhr_ISD_by] Like 'ES-8000-00' AND [ndr_TP] Like 'FV' OR [ndr_TP] = 'LV' OR [ndr_TP] = 'PV' OR [ndr_TP] = 'TV'"
Look like a typo error or something missing?

JKwan
01-26-2022, 03:32 PM
WHERE [hdr_RD] Like 'New*'
WHERE [hdr_RD] Like '*New'
WHERE [hdr_RD] Like '*New*'

Depends on your string, pick convention from above.
you may need to use % instead of *

Also, I would use brackets as well.... if you don't i think you may run into unexpected results due to logic of AND and OR.

arnelgp
01-26-2022, 08:07 PM
you can also do like this:

SQL = "SELECT [hdr11], [hdr31], [hdr32], [hdr33], [hdr34] FROM " & strTable & _
" WHERE " & _
"[hdr_RD] IN ('New', 'Hold') AND " &
"[nhr_ISD_by] = 'ES-8000-00' AND " & _
"[ndr_TP] IN ('FV', 'LV', 'PV', 'TV');"

Alternsti
01-27-2022, 03:22 PM
arnelgp: your solution is the perfect fit. I was unaware of this way to combine several criterias. Big Thanks!!!

JKwan: I tried those but it wasn't the solution. Thanks!