Menu ▼



Posted By :
avatarmrkraju
Total Views : 340

SQL TRIM() function

In SQL server there is no inbuilt trim function, but we have inbuilt LTRIM() & RTRIM() functions. LTRIM() function will trim the left side (leading)spaces and RTRIM() function will remove right side(trailing) spaces in a string. So by using inbuilt LTRIM() & RTRIM() function we can write the user define TRIM() function to trim the leading and trailing spaces in a string.

Function

CREATE FUNCTION TRIM(@inputStr NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
BEGIN
    RETURN LTRIM(RTRIM(@inputStr))
END

Usage

SELECT dbo.TRIM(' Hello World                                        ')
Output
'Hello World'

Using in SELECT statement

SELECT dbo.TRIM(EmployeeName) FROM tblEmployee






comments powered by Disqus


Search dotnetlearners.com