Can anyone help with SQL?
Author
Discussion

audi321

Original Poster:

6,226 posts

245 months

Tuesday 19th December 2017
quotequote all
So our resident IT guru has left for xmas break and I'm struggling with something which is probably very simple!

It's a where statement. I'm putting (for example):

Select * from XYZ
where ABC = 12345678 or 23456789

and I'm getting a 'Non Boolean' error.

Can anyone suggest where to start looking? It seems straightforward in the table, a list of numbers! If I leave out the 'or 23456789' it will return one line, but as soon as I add the 'or' in, I get the error.

Thanks in advance!

Tigerite

76 posts

183 months

Tuesday 19th December 2017
quotequote all
Select *
from XYZ
where ABC = 12345678
or ABC = 23456789

Or you can do
Select *
from XYZ
where ABC in (12345678, 23456789)

chris285

812 posts

164 months

Tuesday 19th December 2017
quotequote all
try

Select * from XYZ
where ABC in (12345678, 23456789)

I think the issue is you saying it equals and then 2 results which it can't, the above returns results with both conditions

Edit - Beaten to it lol

audi321

Original Poster:

6,226 posts

245 months

Tuesday 19th December 2017
quotequote all
PERFECT thanks guys! Solved in 5 mins! I'd already spent 30 mins on google!

wiggy001

7,417 posts

303 months

Tuesday 19th December 2017
quotequote all
The issue is because

where ABC = 12345678 is a boolean test (is ABC equal to 12345678)

but

or 23456789 isn't a boolean test (or 23456789 equals... what?)

The solution using an IN clause given above is the neatest and most efficient way of writing the statement you need.

otolith

70,113 posts

236 months

Tuesday 19th December 2017
quotequote all
Could have been more baffling, at least SQL uses different operators for bitwise and logical OR.

Dr Mike Oxgreen

4,481 posts

197 months

Tuesday 19th December 2017
quotequote all
audi321 said:
Select * from XYZ
where ABC = 12345678 or 23456789
You’re lucky it didn’t interpret the “23456789” as a non-zero value, and hence a Boolean “true”. The overall “where” clause would have been true regardless of the value of ABC, and the select would simply have returned all rows from the table. That would have been confusing for a beginner to diagnose.