Split string into a table using table valued functions
create Function dbo.fn_split(@Message Varchar(1000), @delimiter char(1), @index int ) Returns @TblSplit Table(Id Int Identity(1,1),Value
)Varchar(500)As
Begin
Declare
@curIndex
@pos
@prevPos
@result
int = 0, int = 1, int = 0, varchar(1000) while @pos > 0 Begin set @pos = CHARINDEX(@delimiter, @Message, @prevPos); if(@pos > 0) begin-- get the chars between the prev position to next delimiter pos
set @result = SUBSTRING(@message, @prevPos, @pos-@prevPos) end else begin--get last delim message
set @result = SUBSTRING(@message, @prevPos, LEN(@message)) end iNSERT INTO @TblSplit(Value) vALUES(@result)
set @prevPos = @pos + 1 set @curIndex = @curIndex + 1; end return; End
test:
select * FROM dbo.fn_SPLIT('D:\DBF_004_11_12_14_V11\FDMS\024.dbf','\', 2)
create Function dbo.fn_split(@Message Varchar(1000), @delimiter char(1), @index int ) Returns @TblSplit Table(Id Int Identity(1,1),Value
)Varchar(500)As
Begin
Declare
@curIndex
@pos
@prevPos
@result
int = 0, int = 1, int = 0, varchar(1000) while @pos > 0 Begin set @pos = CHARINDEX(@delimiter, @Message, @prevPos); if(@pos > 0) begin-- get the chars between the prev position to next delimiter pos
set @result = SUBSTRING(@message, @prevPos, @pos-@prevPos) end else begin--get last delim message
set @result = SUBSTRING(@message, @prevPos, LEN(@message)) end iNSERT INTO @TblSplit(Value) vALUES(@result)
set @prevPos = @pos + 1 set @curIndex = @curIndex + 1; end return; End
test:
select * FROM dbo.fn_SPLIT('D:\DBF_004_11_12_14_V11\FDMS\024.dbf','\', 2)
No comments:
Post a Comment