Case statement

  • I would like use case statement in select .for example, if rate =100, then 1, if it is null, then 1, and if it is <>100, it is 0,

    how to write the select case statement in best way?

    Thanks

  • This would do the trick:

    SELECT

    CASE

    WHEN ISNULL(rate,100)=100 THEN 1

    ELSE 0

    END AS Rate

    FROM {yourtable}

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • sqlfriends (7/24/2013)


    I would like use case statement in select .for example, if rate =100, then 1, if it is null, then 1, and if it is <>100, it is 0,

    how to write the select case statement in best way?

    Thanks

    SELECT

    (CASE

    WHEN rate IS NULL THEN 1

    WHEN rate = 100 THEN 1

    WHEN rate <> 100 THEN 0

    END)

    AS RateCode

  • Thanks both of you

  • Why use a CASE at all?

    WITH Rates (rate) AS (

    SELECT -100 UNION ALL SELECT NULL UNION ALL SELECT 0

    UNION ALL SELECT 100 UNION ALL SELECT 5)

    SELECT rate, 1-ABS(SIGN(100-ISNULL(rate,100)))

    FROM Rates


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (7/24/2013)


    Why use a CASE at all?

    WITH Rates (rate) AS (

    SELECT -100 UNION ALL SELECT NULL UNION ALL SELECT 0

    UNION ALL SELECT 100 UNION ALL SELECT 5)

    SELECT rate, 1-ABS(SIGN(100-ISNULL(rate,100)))

    FROM Rates

    Just one reason but not the reason most who know me might think. On a million rows, the CASE method takes about 187ms (after optimizing for the "most common first" order , 234ms un-optimized) and the FORMULA comes in at about 218ms. While that's about 1/6th slower than the optimized CASE method, that's not the reason why I'd say to use the CASE statement. I say it's simply because the CASE statement will be easier to maintain by an individual not having quite as much formula knowledge. If the formula were MUCH faster across the million rows then, of course, I'd go with the formula and a comment explaining how it worked.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/26/2013)


    dwain.c (7/24/2013)


    Why use a CASE at all?

    WITH Rates (rate) AS (

    SELECT -100 UNION ALL SELECT NULL UNION ALL SELECT 0

    UNION ALL SELECT 100 UNION ALL SELECT 5)

    SELECT rate, 1-ABS(SIGN(100-ISNULL(rate,100)))

    FROM Rates

    Just one reason but not the reason most who know me might think. On a million rows, the CASE method takes about 187ms (after optimizing for the "most common first" order , 234ms un-optimized) and the FORMULA comes in at about 218ms. While that's about 1/6th slower than the optimized CASE method, that's not the reason why I'd say to use the CASE statement. I say it's simply because the CASE statement will be easier to maintain by an individual not having quite as much formula knowledge. If the formula were MUCH faster across the million rows then, of course, I'd go with the formula and a comment explaining how it worked.

    Buzz-kill! 😀

    But you're right of course.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply