RTRIM and LTRIM??

  • I am trying to understand how to use RTRIM and LTRIM.

    Example: FirstName LastName - how do you remove the space between FirstName and LastName to make it FirstNameLastName.

    Thank you in advance for your assistance.

  • If 'FirstName LastName' is in a single column, then...

    SELECT REPLACE(namecolumn,' ','')

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

  • Thanks!!! 🙂

  • If in multiple columns and you've got leading or trailing spaces:

    CREATE TABLE #NAMES

    (FirstName varchar (25),

    LastName varchar (25))

    INSERT INTO #NAMES

    SELECT 'Bob ','Johnson ' UNION

    SELECT ' Bob',' Johnson' UNION

    SELECT ' Bob ',' J. Johnson '

    SELECT FirstName, LastName FROM #NAMES

    SELECT RTRIM (LTRIM(Firstname)) , RTRIM(LTRIM (LastName)) FROM #NAMES

    SELECT RTRIM (LTRIM(Firstname)) + RTRIM(LTRIM (LastName)) FROM #NAMES

    SELECT RTRIM (LTRIM(Firstname + LastName)) FROM #NAMES

    DROP TABLE #NAMES

    Todd Carrier
    MCITP - Database Administrator (SQL 2008)
    MCSE: Data Platform (SQL 2012)

  • s43s (3/18/2008)


    Thanks!!! 🙂

    You bet... thank you for the feedback. If you want to learn more, take a look at Todd's code. It's a good example of what the trim functions can actually do.

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

Viewing 5 posts - 1 through 4 (of 4 total)

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