Sunday, June 1, 2014

How to remove the long space in the sentence using SQL SERVER

Step 1:- Example sentence

'Hello               Welcome   to   lokeshasp.blogspot.com'
Step2:-Write the below code
DECLARE @str varchar(150)

SET @str='Hello               Welcome   to   lokeshasp.blogspot.com'

Select REPLACE(REPLACE(REPLACE(@str,' ','[]'),'][',''),'[]',' ')
OutPut :-
Hello Welcome to lokeshasp.blogspot.com

Explanation
DECLARE @str varchar(150)

SET @str='Hello               Welcome   to   lokeshasp.blogspot.com'

Select REPLACE(@str,' ','{}')

O/P:-  Hello{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}Welcome{}{}{}to{}{}{}lokeshasp.blogspot.com
Select REPLACE(REPLACE(@str,' ','{}'),'}{','')

O/P:-   Hello{}Welcome{}to{}lokeshasp.blogspot.com
Select REPLACE(REPLACE(REPLACE(@str,' ','{}'),'}{',''),'{}',' ')

O/P:-   Hello Welcome to lokeshasp.blogspot.com

Full Code
DECLARE @str varchar(150)
SET @str='Hello               Welcome   to   lokeshasp.blogspot.com'
Select REPLACE(@str,' ','{}')
Select REPLACE(REPLACE(@str,' ','{}'),'}{','')
Select REPLACE(REPLACE(REPLACE(@str,' ','{}'),'}{',''),'{}',' ')


No comments:

Post a Comment