24

I'm looking for a way to build case statements in a sql select query using less than and greater than signs. For example, I want to select a ranking based on a variable:

DECLARE @a INT
SET @a = 0

SELECT CASE 
         WHEN @a < 3 THEN 0
         WHEN @a = 3 THEN 1
         WHEN @a > 3 THEN 2
       END

I'd like to write it as:

DECLARE @a INT
SET @a = 0

SELECT CASE @a
         WHEN < 3 THEN 0
         WHEN 3 THEN 1
         WHEN > 3 THEN 2
       END

...but SQL doesn't let me use the < and > signs in this way. Is there a way that I can do this is SQL 2005, or do I need to use the code like in the first one.

The reason for only wanting the code there once is because it would make the code a lot more readable/maintainable and also because I'm not sure if SQL server will have to run the calculation for each CASE statement.

I'm looking for a VB.NET case statement equivelent:

Select Case i
    Case Is < 100
        p = 1
    Case Is >= 100
        p = 2
End Select

Maybe it's not possible in SQL and that's ok, I just want to confirm that.

6
  • 2
    The CASE is working like CASE or SWITCH in most other programming languages. I don't see the point of your need.
    – OMG Ponies
    Feb 27, 2012 at 5:19
  • Sorry, the end of my question was cut off. Let me know if you still don't understand what I'm trying to achieve
    – Greg
    Feb 27, 2012 at 5:35
  • The CASE expression will short-circuit and evaluate cases only until and if one of the is true. I don’t think there’s any advantage to another approach.
    – Steve Kass
    Feb 27, 2012 at 5:54
  • I understand that it will only pick the first case that evaluates to true, my problem is that I want it to evaluate true based on a less than or greater than sign, not evaluate on being equal to a specific value. In this example there is no problems with writting WHEN @a < 3, but in my actual code @a is a long and complicated equation. I don't want to maintain 3 copies of it and I don't want SQL to run it 3 times for each record. I'd prefer to do it like VB.Net allows if possible
    – Greg
    Feb 27, 2012 at 6:06
  • 2
    As I've mentioned a couple of times my code isn't as simple as @a, it's actually a complicated equation: (((Col1 * Col2) + col3-col2)/col4). This needs to be calculated for each row. I agree that the first statement works perfectly, but when you add in the length of the actual column names it becomes hard to read
    – Greg
    Feb 28, 2012 at 5:43

3 Answers 3

26

You can use the SIGN function as

DECLARE @a INT
SET @a = 0

SELECT CASE SIGN(@a - 3)
         WHEN -1 THEN 0
         WHEN 0 THEN 1
         WHEN 1 THEN 2
       END

If @a is smaller than 3, then @a - 3 results in a negative int, in which SIGN returns -1.

If @a is 3 or greater, then SIGN returns 0 or 1, respectively.


If the output you want is 0, 1 and 2, then you can simplify even more:

DECLARE @a INT
SET @a = 0

SELECT SIGN(@a - 3) + 1
3
  • I fail to see how this is more maintainable. Anyone who knows SQl will find the orginal statement that the OP didn't like but which works to be clearer.
    – HLGEM
    Feb 27, 2012 at 20:37
  • 1
    @HLGEM I would also agree with you if @a was just a simple expression, but according to OP, @a is a long complicated expression that he doesn't want to see repeated three times in a case statement. So, I guess my or Andriy M's solution are appropriate to solve his problem. Feb 28, 2012 at 5:43
  • 1
    Well, for me this worked perfectly. I had to determine a VAT percentage that has recently changed in the Netherlands. I simply compared the delivery date of the product with the date at which the new VAT percentage should be used (using the datediff function) and then used the sign() construction to determine the correct VAT percentage. Seems like an elegant enough solution to me.
    – Jela
    Nov 8, 2012 at 9:04
16

Using SIGN as suggested by @Jose Rui Santos seems a nice workaround. An alternative could be to assign the expression an alias, use a subselect and test the expression (using its alias) in the outer select:

SELECT
  …,
  CASE
    WHEN expr < 3 THEN …
    WHEN expr > 3 THEN …
  END AS …
FROM (
  SELECT
    …,
    a complex expression AS expr
  FROM …
  …
)
3
SELECT 
CASE 
WHEN ColumnName >=1 and ColumnName <=1 THEN 'Fail'
WHEN ColumnName >=6 THEN 'Pass'
ELSE 'Test'
END
FROM TableName
2
  • the original question was how do I get the condition to not appear on every line. This doesn't actually answer the question as I would still have to repeat equation on 2 of the lines and assume the else
    – Greg
    Jan 16, 2013 at 23:13
  • I can't upvote this because it doesn't necessarily solve original question, but it solved my issue. Thanks. Just needed to move the ColumnName off the "CASE" line to each of the "WHEN" lines.
    – Ads
    May 18, 2017 at 5:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.