I have split function,in the following way
create function split (@list varchar(max), @delimiter char(1))
returns @shards table (value varchar(8000))
with schemabinding
as
begin
declare @i int;
set @i = 0;
while @i <= len(@list)
begin
declare @n int;
set @n = charindex(@delimiter, @list, @i);
if 0 = @n
begin
set @n = len(@list);
end
insert into @shards (value)
values (substring(@list, @i, @n-@i+1));
set @i = @n+1;
end
return;
end
Using above function we can pass only one column name at a time,
But my requirement is to pass more than one column Names to split function.
is it possible?
Any thoughts Help me
Loading
Benjamin KemnerPosted Sep 6, 2011, 3:43 AM
First extend your funciton to keep an identifier in your table(@c).
create function split (@list varchar(max), @delimiter char(1))
returns @shards table (id integer, value varchar(8000))
with schemabinding
as
begin
declare @i int;
declare @c int;
set @i = 0;
set @c = 0;
while @i <= len(@list)
begin
declare @n int;
set @n = charindex(@delimiter, @list, @i);
if 0 = @n
begin
set @n = len(@list);
end
insert into @shards (id, value)
values (@c, substring(@list, @i, @n-@i+1));
set @i = @n+1;
set @c = @c+1;
end
return;
end
Than you can select from your function with different values. You have to join the ids to prevent from crossed results.
select split1.value, split2.value, split3.value, split4.value from
split('a,b,c', ',') as split1,
split('a,b,c', ',') as split2,
split('1,2,3', ',') as split3,
split('some,more,values', ',') as split4
where split1.id = split2.id
and split1.id = split3.id
and split1.id = split4.id
srinivas PPosted Sep 6, 2011, 2:48 AM
Benjamin KemnerPosted Sep 5, 2011, 9:27 AM
regards