GREATEST and LEAST function

  • SQLArnold - Thursday, June 1, 2017 9:03 AM

    dmartin 38210 , I am a huge fan of Itzik. This is a great option, thanks!

    If you like that solution, you might want to read about how it actually works.  Please see the following.
    http://www.sqlservercentral.com/articles/Tally+Table/72993/

    --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)

  • By taking the best of all the responses, seems like this works for any datatype and is easy enough to do without typing the same value/variable over and over:


    select [Greatest] = max(t.x)
         , [Least] = min(t.x)
    from (values (123), (23), (null), (77)) as t(x)

    You can use variables, scalar function calls etc.

Viewing 2 posts - 16 through 16 (of 16 total)

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