Answer the question
In order to leave comments, you need to log in
How to make a BETWEEN check between a larger and smaller number?
Good afternoon!
Temporary creative block.
How to make BETWEEN check between a larger number and a smaller one.
For example, between 20 and 30 everything is ok - it works.
And between 30 and 20 is an empty result.
I do not want to make a condition which variable is larger and change places in the query. Any ideas?
Answer the question
In order to leave comments, you need to log in
Add through OR the second line BETWEEN with the changed arguments. You can use the CASE structure in the query.
I don’t want to make a condition which variable is larger and change places in the query
Can be moved to scalar variables
Something like
DECLARE @minValue INT, @maxValue INT;
IF (@v1 < @v2)
BEGIN
SET @minValue = @v1;
SET @maxValue = @v2;
END
ELSE
BEGIN
SET @minValue = @v2;
SET @maxValue = @v1;
END
SELECT *
FROM table
WHERE v BETWEEN @minValue AND @maxValue
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question