首页 > 数据库 > SQL Server > 正文

MS SQL Server2k字符串分拆与合并处理代码

2024-08-31 00:49:12
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,         -- ms sql server2k字符串分拆与合并处理代码             

 --字符串分拆处理代码
create function f_splitstr(
@s     varchar(8000),  --待分拆的字符串
@split  varchar(10)     --数据分隔符
)returns table
as
--select top 8000 id=identity(int,1,1) into dbo.tb_splitstr  --字符串分拆函数要用到的辅助表.
--from syscolumns a,syscolumns b

return(
 select col=cast(substring(@s,id,charindex(@split,@[email protected],id)-id) as varchar(100))
 from tb_splitstr
 where id<=len(@s+'a')
  and charindex(@split,@[email protected],id)=id)
--drop table tb_splitstr
go

/* --利用以上函数,对某一表的字符串字段字符分拆处理代码
declare @id int,@c varchar(50)
declare k cursor for select * from id_f --字符串待分拆原始表(id int,c varchar(50))
open k
fetch next from k into @id,@c
while (@@fetch_status=0)
 begin
   insert into id_yf --分拆结果表(id int,c varchar(50))
   select @id as id,c.* from f_splitstr(@c,',') as c
   fetch next from k into @id,@c
 end
close k
deallocate k
*/
--------------------------------------------------------------------------
--字符串合并处理代码
declare @s varchar(300)
set @s=''
select @[email protected]+c+','
from [dbo].[id_h] --字符串待合并表[id int,c varchar(50)]
where id <> (select top 1 id from id_h order by id desc) order by id

select @[email protected]+ c from id_h where id =(select top 1 id from id_h order by id desc)
select @s

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表