• 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