[Irving]SqlServer 拆分函数用法

drop function dbo.split  删除自定义函数 dbo.split  函数名

函数 dbo.split
create function dbo.split
(
@c varchar(1000),
@split varchar(2)
)
returns @t table(name varchar(100))
as
begin
  while(charindex(@split,@c)<>0)
    begin
          insert @t(name) values(substring(@c,1,charindex(@split,@c)-1))
          set @c=stuff(@c,1,charindex(@split,@c),'')
    end
  insert @t(name)values(@c)
return
end
函数详解:
   创建sql函数传入要拆分的字符串和拆分符号,判断第一个拆分符号位置,提取这一段字符插入临时表,把这一段字符清空,
循环对剩下的字符串进行处理

insert into test(name)select name from dbo.split('asd,sdf,s,sdf,45,123456,sdf',',')
语句详解
从函数返回表中提取列插入到指定表的列中

原文地址:https://www.cnblogs.com/teamate/p/3677763.html