question

prasad.nyalapatla avatar image
prasad.nyalapatla asked

how to use BETWEEN Operator for negative values

We had one table, one of column contains negative and positive values, how can we get required data by using BETWEEN operator? Example we are using below syntax, SELECT * FROM FactTable WHERE Lats between 5 and -5 (edited by moderator to try to get the question readable...)
between
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
This web site runs on voting. Please indicate all helpful answers by clicking on the thumbs up next to those answers. If any one answer lead to a solution, please indicate this by clicking on the check mark next to that answer.
0 Likes 0 ·
virtualjosh avatar image
virtualjosh answered
Your range is incorrect. There are no numbers greater than or equal to 5 and less than or equal to -5. Use the below instead. BETWEEN -5 AND 5
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
To clarify @virtualjosh's answer, the `BETWEEN` operator works like this: SELECT * FROM FactTable WHERE Lats BETWEEN x AND y can be thought of as equivalent to: SELECT * FROM FactTable WHERE Lats >= x AND Lats
1 Like 1 ·
Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
If your 5 and -5 are not constants, but rather variables, and you don't know beforehand which one is the bigger number of the two variables, you could use something like this: SELECT * FROM FactTable WHERE Lats BETWEEN CASE WHEN @variable1
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.