We will create a function called Trim as below and use the Ltrim and Rtrim in-built Sql Server function. SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [TRIM](@string VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(@string))
END
To use, SELECT dbo.TRIM(' sample text Here ');