• vince_sql (10/23/2011)


    daveriya (10/21/2011)


    hi

    i have this code in where clause RTRIM(LTRIM(ODER_ID))= ' ',WHAT THis do

    It's just removing extra blank spaces, but this is un-necessary as SQL views empty strings as empty strings regardless how many spaces there are.

    So if your query is trying to return only empty strings, then you dont need to trim.

    The code below highlights this, if SQL considered

    '' != ' '

    the above to be true, then the penultimate select query would only return 1 row, but it returns 2.

    CREATE TABLE trimTest (

    value varchar(50)

    )

    INSERT INTO trimTest

    VALUES (''),

    (' ')

    SELECT *

    FROM trimTest

    SELECT *

    FROM trimTest

    WHERE value = ''

    SELECT *

    FROM trimTest

    WHERE LTRIM(RTRIM(value)) = ''

    AND, Vince's query uses a SARGable WHERE clause, as well!

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