Search code examples
sqlcaseinequality

SQL Case inequality


Currently I am using

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

How can I achieve the same effect in a single query?


Solution

  • You simply put the condition in the query:

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

    Be warned though; this can confuse the optimizer slightly which might make your first query less performant. As with everything test before implementation.