SQL分割字符串函数


--SQL分割字符串函数的调用查询语句

--输出结果为数据表

Declare @strPrimaryKey nvarchar(500)
set @strPrimaryKey='1,2,3,4,5,6,7'
Declare @strPlitChar nvarchar(10)
set @strPlitChar=','
Declare @strReturn nvarchar(500)

select * from  dbo.fun_SplitStr(@strPrimaryKey,@strPlitChar)

--SQL分割字符串函数
CREATE FUNCTION dbo.fun_get_SplitStr
(
 @SourceSql    varchar(8000),
 @StrSeprate    varchar(100))
 returns @temp   table(F1 varchar(100)
)  
AS    
BEGIN  
  declare ch   as    varchar(100)  
  set     @SourceSql =@SourceSql+@StrSeprate    
  while(@SourceSql<>'')  
  BEGIN
        set      @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)  
   insert   @temp   values(@ch)  
   set      @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')  
  END  
  return  
END

posted on 2009-04-22 19:03  风灵溪清  阅读(180)  评论(0编辑  收藏  举报

导航