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

排列组合数据转换问题
有一组数据‘100110000111001101000000010101’
‘按顺序’转换数据。1》是、01》是、001》是、000》非
达到转换要求的是‘是是是非是是是是是是非非是是是’
----
以下代码仅供参考。

create   table   #1   (
id   int   identity,
tmp   varchar(40)
)
insert   into   #1(tmp)   values( '100110000111001101000000010101 ')
select   identity(int,1,1)   as   id,replace(replace(replace(tmp, '111 ', '非 '), '1 ', ' '), '0 ', '是 ')   as   tmp
into   #2
from   #1
select   *   from   #2

------解决方案--------------------
declare @str varchar(100), @result varchar(100)
select @str= '100110000111001101000000010101 ', @result= ' '
while len(@str)> 0
begin
if(substring(@str, 1, 1)= '1 ')
begin
select @result=@result+ '是 ', @str=stuff(@str, 1, 1, ' ')
end
else if(substring(@str, 1, 2)= '01 ')
begin
select @result=@result+ '是 ', @str=stuff(@str, 1, 2, ' ')
end
else if(substring(@str, 1, 3)= '001 ')
begin
select @result=@result+ '是 ', @str=stuff(@str, 1, 3, ' ')
end
else if(substring(@str, 1, 3)= '000 ')
begin
select @result=@result+ '非 ', @str=stuff(@str, 1, 3, ' ')
end
else
select @str=stuff(@str, 1, 1, ' ')
end

select @result

--result
是是是非是是是是是是非非是是是

(1 row(s) affected)