I had faced white space problem writing algorithm of search in SQL Query, I had applied there FTS(Full-Text Search) when text come with extra space my search become failed. I applied there LTRIM and RTRIM but did not get success then I applied bellow approach and get success over there.
DECLARE @str VARCHAR(200)
DECLARE @expres VARCHAR(50) = SPACE(2)
SET @str = 'I am trying to remove extra space'
WHILE CHARINDEX( @expres, @str ) > 0
SET @str = REPLACE( @str, @expres,' ')
Above query you can use as a function or don’t want use as a function and want to apply direct approach see below code.
-- Symple query to remove extra space
DECLARE @str VARCHAR(200)='I am trying to remove extra space'
SELECT REPLACE (REPLACE(REPLACE(@str,' ','<>'),'><',''),'<>',' ')