删除SQL Server中字符串之间发生的所有空格

时间:2015-07-02 09:24:23

标签: sql sql-server sql-server-2008 whitespace

我的数据如下:

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. 

在这里,我有2列名为IDMeta。在Meta列中,数据(字符串)中包含空格。我想删除它,并应显示如下:

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. 

我已经尝试了Replace功能,但它无效,以及以下脚本:

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

但没有运气。

帮助。!!感谢

3 个答案:

答案 0 :(得分:0)

也许是标签而不是空格。 你试过了吗?

SELECT REPLACE([Meta], char(9), '') From [dbo.MetaTags]

答案 1 :(得分:0)

这很脏,因为我讨厌循环和行基本操作 - 但至少它有效:

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

答案 2 :(得分:0)

试试这个,

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,'  ',','),',,',' '),',',' ')