首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

排列组合数据转换有关问题

2012-02-13 
排列组合数据转换问题有一组数据‘100110000111001101000000010101’‘按顺序’转换数据。1》是、01》是、001》是、00

排列组合数据转换问题
有一组数据‘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)

热点排行
Bad Request.