t-sql trim function

Written by Alex on . Posted in Uncategorized

I was surprised when found than SQL server 2005 and even 2008 R2 does not have support for trim function. Trim function is so simple and wide use function so I believed all programming languages support it. But instead Sql server has ltrim and rtrim function

  • ltrim – returns string after removing leading blank characters
  • rtrim – return string after removing trailing blank characters.

But using these two functions you can create trim function and use it in your t-sql code

t-sql trim function

You can create trim function for your database using this source code

CREATE FUNCTION [dbo].[trim] (@stringValue as nvarchar(max))
RETURNS nvarchar(max)
AS
BEGIN
    RETURN ltrim(rtrim(@stringValue));
END

t-sql trim inline code

One more option is to use inline code if for some reason you don’t want to create function

ltrim(rtrim(LastName)) + ', '
+ ltrim(rtrim(FirstName)) as 'Customer Name',

Tags: ,

Trackback from your site.

Leave a comment