Wrong inequality

I am currently using

If @City > 0 then
    SELECT * FROM table1 Where Column1 < @City
ELSE
    SELECT * FROM table1

      

How can I achieve the same effect in one request?

+3


source to share


3 answers


You just put a condition in your query:

select *
  from table1
 where ( @city > 0
         and column1 < @city )
    or @city <= 0 

      



Be careful; this can confuse the optimizer a bit and make your first query less efficient. Like all tests before implementation.

+4


source


Try the following:



select *
  from table1
 where column1 < @city
    or @city <= 0 

      

0


source


Another option

SELECT * 
FROM table1 
WHERE Column1 < CASE WHEN @City > 0 
                     THEN @City 
                     ELSE Column1 + 1 END

      

0


source







All Articles