--字符串分拆處理代碼
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
新聞熱點
疑難解答