日期:2014-05-18  浏览次数:20466 次

sql语句中对字符串的处理问题
有这样一个字符串:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,

当我把1,替换成空的时候,后面的11,12就变成112了,造成结果错误。

而我希望得到的正确结果是
2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
...

我写的语句是:
set   @centers   =   Replace(@centers,@center   +   ', ',   ' ')
@centers就是长长的字符串,@center是第一个数字。

请问有什么解决办法呀?


------解决方案--------------------
set @centers = Replace( ', '+@centers+ ', ', ', '+@center + ', ', ' ')
------解决方案--------------------
--try

declare @centers nvarchar(100)
set @centers= '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, '
set @centers = Replace( ', '+@centers, ',1, ', ' ')
select @centers
------解决方案--------------------
set 字符串 = replace( ', ' + 字符串 , ',1, ' , ' ')
------解决方案--------------------
--@center是第一个数字?这样?

declare @centers varchar(100)
set @centers= '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, '
while len(@centers)> 0
begin
print @centers
set @centers=right(@centers,len(@centers)-charindex( ', ',@centers))
end
/*
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
5,6,7,8,9,10,11,12,13,14,15,16,17,18,
6,7,8,9,10,11,12,13,14,15,16,17,18,
7,8,9,10,11,12,13,14,15,16,17,18,
8,9,10,11,12,13,14,15,16,17,18,
9,10,11,12,13,14,15,16,17,18,
10,11,12,13,14,15,16,17,18,
11,12,13,14,15,16,17,18,
12,13,14,15,16,17,18,
13,14,15,16,17,18,
14,15,16,17,18,
15,16,17,18,
16,17,18,
17,18,
18,
*/
------解决方案--------------------
楼上的太对了
------解决方案--------------------
set @centers = Replace(@centers,@center + ', ', ' ')
set @centers=Replace(@centers, ',112, ', ',11,12, ')