SQL Server Split function
USE [A5GODB]
GO
/****** Object: UserDefinedFunction [dbo].[SPLIT_FUNC] Script Date: 10/23/2014 10:08:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[SPLIT_FUNC](@string nvarchar(1000),@spliter char(1)=',')
returns @table table(id int identity(1,1) primary key,items varchar(100) not null)
AS
begin
if patindex('%'+@spliter+'%',@string)=1
begin
set @string=substring(@string,2,len(@string))
end
if patindex('%'+@spliter+'%',reverse(@string))=1
begin
set @string=reverse(substring(reverse(@string),2,len(@string)))
end
while patindex('%'+@spliter+'%',@string)>0
begin
declare @file varchar(1000)
set @file=substring(@string,0,patindex('%'+@spliter+'%',@string))
insert into @table(items)
select @file
set @string=substring(@string,patindex('%'+@spliter+'%',@string)+1,len(@string))
end
if patindex('%'+@spliter+'%',@string)=0
begin
select @file=substring(@string,0,len(@string)+1)
insert into @table(items)
select @file
end
return
end
SQL Server Split function
No comments:
Post a Comment