0

My data looks like:

ID  <--column name
1   

Meta <--column name
Is eating chocolate really as bad as        they say it is? Learn the surprising new findings on chocolate as        they relate to health. 

ID <--column name
2
Meta<--column name
Osteoporosis is more common in people with celiac disease.        Find out what simple and safe solutions are available. 

Here, i have 2 columns named ID and Meta. In Metacolumn the data(string) contains whitespaces in between. i want to remove it, and should show like below:

ID  <--column name
1   

Meta <--column name
Is eating chocolate really as bad as they say it is? Learn the surprising new findings on chocolate as they relate to health. 

ID <--column name
2
Meta<--column name
Osteoporosis is more common in people with celiac disease.Find out what simple and safe solutions are available. 

I've tried Replace function but it is not working and also this below script :

Declare @InputStr varchar(8000)
declare @ResultStr varchar(8000)
set @ResultStr = (select top 2 Meta from MetaTags)
Print @ResultStr
while charindex('', @ResultStr) > 0
    set @ResultStr = replace(@InputStr, '  ', '  ')
Print @ResultStr

But no luck.

Help.!! Thanks

3 Answers3

0

Maybe it is tab and not spaces. Did you try :

SELECT REPLACE([Meta], char(9), '') From [dbo.MetaTags]
chipou
  • 45
  • 1
  • 4
0

This is quite dirty as i hate loops and row base operations - but at least it works:

DECLARE @tab TABLE
(
    ID INT,
    Meta NVARCHAR(200)
)

INSERT INTO @tab VALUES
(1,'Is eating chocolate really as bad as        they say it is? Learn the surprising new findings on chocolate as        they relate to health. '),
(2,'Osteoporosis is more common in people with celiac disease.        Find out what simple and safe solutions are available. ')

SELECT * FROM @tab

DECLARE @i INT = 1

WHILE @i <= 10
BEGIN
    UPDATE @tab SET Meta = REPLACE(Meta,(' '),'')
    SET @i = @i + 1
END

SELECT * FROM @tab
CeOnSql
  • 2,615
  • 1
  • 16
  • 38
  • not working because? are you sure you have spaces in your text? not tabs, other blanks,...? if you copy my example code and execute it - it works! – CeOnSql Jul 02 '15 at 09:45
  • I think it is a tab... –  Jul 02 '15 at 09:48
  • then the update could look like this: `UPDATE @tab SET Meta = REPLACE(REPLACE(Meta,' ',''),CHAR(9),'')` – CeOnSql Jul 02 '15 at 10:25
0

Try this,

DECLARE @V VARCHAR(4000)='Is eating chocolate really as   bad as        they say it is? Learn the surprising new findings on chocolate as        they relate to health. '
SELECT Replace(REPLACE(REPLACE(@V,'  ',','),',,',' '),',',' ')
PP006
  • 681
  • 7
  • 17