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
SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.
TRIM is a function that takes a character expression and returns that expression with leading and/or trailing pad characters removed. Optional parameters indicate whether leading, or trailing, or both leading and trailing pad characters should be removed, and specify the pad character that is to be removed.
By default, the TRIM function removes the space character from both the start and the end of the string. This behavior is equivalent to LTRIM(RTRIM(@string)) .
LTrim() and RTrim() Function in MS AccessIn LTrim() function a string will be pass as a parameter and it will return the string with no leading spaces. 2. RTrim() Function : It works same like LTrim() function but it will remove all trailing spaces from a string.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With