Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi,

based on count of special character '|' in string ,show substring data in sperate column.

my string =12,USA|10,Canada|

desired output= 12 USA 10 Canada
ie.each in seperate columns
12 in one column
USA in one column
10 in one Column
canada in one column

based one , where value and name is seperated and by using | set of value and name is seperated.

thanks
karthik
Posted
Comments
Sergey Alexandrovich Kryukov 4-Jun-12 3:22am    
What do you think is "special character"? There is no such thing. How come you have this? Why?
--SA

1 solution

u can use split fuction which will return same result as .net string().split()


SQL
CREATE FUNCTION [dbo].[Split](@String varchar(max), @Delimiter char(1))
returns @temptable TABLE (items varchar(max))
as
begin
    declare @idx int
    declare @slice varchar(max)

    select @idx = 1
        if len(@String)<1 or @String is null  return

    while @idx!= 0
    begin
        set @idx = charindex(@Delimiter,@String)
        if @idx!=0
            set @slice = left(@String,@idx - 1)
        else
            set @slice = @String

        if(len(@slice)>0)
            insert into @temptable(Items) values(@slice)

        set @String = right(@String,len(@String) - @idx)
        if len(@String) = 0 break
    end
return
end


this function return a table

u call it as
select * from databaseName.dbo.split(12,USA|10,Canada|','|')

and again split by ',' as follows. for your desiring result.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900