Search code examples
sqlsql-servert-sql

TRIM is not a recognized built-in function name


For the following code:

DECLARE @ss varchar(60)
  SET @ss = 'admin'

  select TRIM(@ss)

I've got an error:

'TRIM' is not a recognized built-in function name


Solution

  • TRIM is introduced in SQL Server (starting with 2017).

    In older version of SQL Server to perform trim you have to use LTRIM and RTRIM like following.

    DECLARE @ss varchar(60)
      SET @ss = ' admin '
    
      select RTRIM(LTRIM(@ss))
    

    If you don't like using LTRIM, RTRIM everywhere, you can create your own custom function like following.

       CREATE FUNCTION dbo.TRIM(@string NVARCHAR(max))
        RETURNS NVARCHAR(max)
         BEGIN
          RETURN LTRIM(RTRIM(@string))
         END
        GO