SQL 2017: Trim me up
Author: jason
Date: 2019-01-17 11:19:33
Category: Technical

Introduced in SQL 2017 is the TRIM command. No longer do you need to RTRIM and LTRIM. Now you can simply TRIM. TRIM it real good

https://docs.microsoft.com/en-us/sql/t-sql/functions/trim-transact-sql?view=sql-server-2017

Code


/*
TRIM, finally, just TRIM
*/

DROP TABLE IF EXISTS #Temp;
CREATE TABLE #Temp ([Name] VARCHAR(25), [Description] VARCHAR(255));
INSERT #Temp ([Name], [Description]) SELECT LTRIM(RTRIM(' Apple ')), LTRIM(RTRIM(' Green ')); --LTRIM,RTRIM
INSERT #Temp ([Name], [Description]) SELECT ' Apple ', ' Red '; --Empty Spaces!!
INSERT #Temp ([Name], [Description]) SELECT TRIM(' Plum '), TRIM(' Purple '); --TRIM
INSERT #Temp ([Name], [Description]) SELECT TRIM(' Orange '), TRIM(' Orange '); --TRIM

SELECT STRING_AGG([Name], ',') AS NewMagical FROM #Temp; --Noting the before/after spaces in the middle







jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009