Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a substring that returns from a split function

I have a string like below

a_b|c_d

and I need to split it based on |.

So, the result will be:

a_b
c_d

Then, I want to split it again with _, then result will be:

a
b

c
d

Is it possible to do this in a single step? Like when input the corresponding string, it will return a value like following:

a
b
c
d

I have created a function for split:

select items from dbo.splitdetails('a_b|c_d','|')

It will result:

a_b
c_d

But I don't know how can I proceed the next split with these results?

Using temporary table, I hope I can do this, but I need to use this inside a function. So I think temporary table is not a good option. Cursor is also an option but when I use cursor, it will degrade the performance because I have thousands of records.

My input is:

a_b|c_d

And desired out put is:

a
b
c
d
like image 462
Arunprasanth K V Avatar asked May 20 '15 05:05

Arunprasanth K V


2 Answers

You can better use Sql Replace function first and then use your split function as below

select REPLACE('a_b|c_d','|','_')

results: a_b_c_d

Now use your Split function like below

select items from dbo.splitdetails('a_b_c_d','_')

EDIT:

All at a time

select items from dbo.splitdetails(REPLACE('a_b|c_d','|','_'),'_')

EDIT1:

In that case Use the Replace option in Reverse

select items from dbo.splitdetails(REPLACE('a_b|c_d','_','|'),'|')
like image 134
Sai Kalyan Kumar Akshinthala Avatar answered Oct 22 '22 01:10

Sai Kalyan Kumar Akshinthala


You can use CROSS APPLY like this.

SELECT d.item,e.item from dbo.splitdetails('a_b|c_d','|') d
CROSS APPLY dbo.splitdetails(d.item,'_') e
like image 24
ughai Avatar answered Oct 22 '22 01:10

ughai