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

求SQL语句,高手们来帮小弟我拆字段

2012-04-13 
求SQL语句,高手们来帮我拆字段现表里有一列,存有如下这样的数据5612@朱岚5708@王洁琪5117@刘桂珍5594@

求SQL语句,高手们来帮我拆字段
现表里有一列,存有如下这样的数据


5612@朱岚;
5708@王洁琪;5117@刘桂珍;
5594@徐婷;5708@王洁琪;5426@谢静佳;5091@刘照晶;
.
.
.
.
都是这样的格式 工号+@+人名,多个人就用;隔开的
每一行可能会存不止一个人名,我现要把拆成

朱岚;
王洁琪;刘桂珍;
徐婷;王洁琪;谢静佳;刘照晶;
这样的  

求解!

[解决办法]

SQL code
create table tb(col varchar(100))insert into tb values('5612@朱岚;')insert into tb values('5708@王洁琪;5117@刘桂珍;')insert into tb values('5594@徐婷;5708@王洁琪;5426@谢静佳;5091@刘照晶;')gocreate function dbo.f_str(@col varchar(100)) returns varchar(100)asbegin  declare @str varchar(100)  declare @i as int  declare @j as int  set @str = ''  set @i = 1  set @j = len(@col) - len(replace(@col , ';' , ''))  while @i <= @j  begin    set @str = @str + substring(@col , charindex('@',@col) + 1 , charindex(';',@col) -  charindex('@',@col) )    set @col = substring(@col , charindex(';',@col) + 1 , len(@col))    set @i = @i + 1  end  return @strendgo--调用函数select newcol = dbo.f_str(col) from tbdrop function dbo.f_strdrop table tb/*newcol                   -------------------------朱岚;王洁琪;刘桂珍;徐婷;王洁琪;谢静佳;刘照晶;(所影响的行数为 3 行)*/ 

热点排行