locked
trim RRS feed

  • Question

  • Hi,

    Is 'trim' not supported in the scripts of SQL Server?

    Friday, December 12, 2008 5:13 AM

Answers

  • No, but you can use RTrim() and LTrim() to trim the right and left of a string.  You could also create a trim function if you wanted.  Something like this would work:

     

    ;CREATE FUNCTION dbo.Trim(@StringToTrim nvarchar(2000))  
    RETURNS nvarchar(2000)  
    AS 
    BEGIN 
        RETURN LTrim(RTrim(@StringToTrim))  
    END 

    Does this help?
    • Proposed as answer by Sankar Reddy Saturday, December 20, 2008 11:29 PM
    • Marked as answer by Mangal Pardeshi Monday, December 22, 2008 9:16 AM
    Friday, December 12, 2008 6:08 AM

All replies

  • No, but you can use RTrim() and LTrim() to trim the right and left of a string.  You could also create a trim function if you wanted.  Something like this would work:

     

    ;CREATE FUNCTION dbo.Trim(@StringToTrim nvarchar(2000))  
    RETURNS nvarchar(2000)  
    AS 
    BEGIN 
        RETURN LTrim(RTrim(@StringToTrim))  
    END 

    Does this help?
    • Proposed as answer by Sankar Reddy Saturday, December 20, 2008 11:29 PM
    • Marked as answer by Mangal Pardeshi Monday, December 22, 2008 9:16 AM
    Friday, December 12, 2008 6:08 AM
  • Aaron,
    How would using a scalar UDF compare performance wise compared to simply SELECT LTrim(RTrim(my_column))?


    George
    Friday, December 12, 2008 12:03 PM
    Answerer
  • George,

    Performance wise I wouldn't expect any difference between the two. Its a matter of nesting the function once and reusing or doing the same over & over.

    Sankar Reddy | http://sankarreddy.spaces.live.com/
    Wednesday, December 17, 2008 12:59 AM