Skip to content

Instantly share code, notes, and snippets.

@somendra007
Created March 27, 2022 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somendra007/b13cb8ce324eabdfce8e492079824fcf to your computer and use it in GitHub Desktop.
Save somendra007/b13cb8ce324eabdfce8e492079824fcf to your computer and use it in GitHub Desktop.
/*
Author : SOMENDRA PRATAP SINGH
select isnull(item, 0), * from service.split1('', ' ', null)
select * from service.split1('ram sham mohan rohan', ' ', null)
select * from split1('ram:23 sham mohan rohan:20', ' ', ':') where item1=23
select * from split2('a@b@c,d@e@,zxcfdsdgdsgsd',',','@','@')
select substring('a-b@c,d-e@f',1,(charindex(',','a-b@c,d-e@f',0)-1))
select substring( substring('a-b@c,d-e@f',1,(charindex(',','a-b@c,d-e@f',0)-1)) ,1,(charindex('-', substring('a-b@c,d-e@f',1,(charindex(',','a-b@c,d-e@f',0)-1)) ,0)-1))
select right(substring('a-b@c,d-e@f',1,(charindex(',','a-b@c,d-e@f',0)-1)),len(substring('a-b@c,d-e@f',1,(charindex(',','a-b@c,d-e@f',0)-1)))-(len('a')+1))
select * from split1( 'b@c','@',null)
*/
create function [dbo].[split2]
(
@str varchar(8000), -- varchar change to 8000 from 1000 by saurabh gaur on 30/03/2006
@spliter char(1),
@colspliter char(1),
@colspliter1 char(1)
)
returns @tname table(item varchar(100), item1 varchar(100),item2 varchar(100))
as
begin
declare @substr as varchar(100),
@value1 as varchar(100),
@value2 as varchar(100),
@value3 as varchar(100),
@sql as varchar(500)
--If condition added by rahul on 15/10/2005
--to return null values if length of Input string is 0
if len(@str) = 0
begin
insert into @tname values(null, null,null)
return
end
--End of Condition added by rahul on 15/10/2005
if charindex(@spliter,@str,0) <> 0
begin
while charindex(@spliter,@str,0)<>0
begin
set @substr=substring(@str,1,(charindex(@spliter,@str,0)-1))
if charindex(@colspliter, @substr, 0) <> 0 and @colspliter is not null and charindex(@colspliter1,@substr,0)<> 0
begin
set @value1 = substring(@substr,1,(charindex(@colspliter,@substr,0)-1))
set @value2=right(@substr,len(@substr)-(len(@value1)+1))
select @value2=item,@value3=item1 from split1(@value2,null,@colspliter1)
insert into @tname (item, item1,item2) values (@value1, @value2,@value3)
end
set @str=right(@str,len(@str)-(len(@substr)+1)) -- as spliter is of one char so +1 reduced
end
SET @substr = @str
if (len(@substr)>3 and charindex(@colspliter,@substr,0) <> 0 and charindex(@colspliter1,@substr,0)<> 0 )
begin
set @value1 = substring(@substr,1,(charindex(@colspliter,@substr,0)-1))
set @value2=right(@substr,len(@substr)-(len(@value1)+1))
select @value2=item,@value3=item1 from split1(@value2,null,@colspliter1)
insert into @tname (item, item1,item2) values (@value1, @value2, @value3)
end
end
else
begin
if @str is not null
BEGIN
if @colspliter is not null --charindex(@str, @colspliter, 0) <> 0 and
begin
set @value1 = substring(@substr,1,(charindex(@colspliter,@substr,0)-1))
set @value2=right(@substr,len(@substr)-(len(@value1)+1))
select @value2=item,@value3=item1 from split1(@value2,null,@colspliter1)
insert into @tname (item, item1,item2) values (@value1, @value2,@value3)
end
END
end
return
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment