Search This Blog

Monday, May 5, 2008

Splitting String using Oracle SQL 9i

Run following query to split values seperated by comma(,)

 SELECT TRIM( SUBSTR ( txt
, INSTR (txt, ',', 1, level ) + 1
, INSTR (txt, ',', 1, level+1) - INSTR (txt, ',', 1, level) -1
)
)
AS token
FROM ( SELECT ','||:in_string||',' AS txt FROM dual )
CONNECT BY level <= LENGTH(txt)-LENGTH(REPLACE(txt,',',''))-1


Example
-------
If value of in_string is entered as 1234,2,3,45,6,7,7,88,9,346

Output is
TOKEN
-----
1234
2
3
45
6
7
7
88
9
346

Reference:
Tom Kyte's Blog

10 Comments:

Lakki Reddy Sreehari Reddy said...

Thank u Suresh for posting this query.Its vey usefull.

Suresh Vaishya said...

Lakki, Thanks for your feedback.

Gaurav Kant Goel said...

Great query!!!

Few other good SQL queries can be found at:

http://infiniteandmore.blogspot.com/

Anonymous said...

Wonderf..
thank you so much...

Anonymous said...

Thank you very much for posting this script!
It's quite simple but not the obvious one!

Alex said...

Thank you very much Suresh ! Your blog is very useful, especially for a beginner like me :)

Tani said...

Thank u Suresh for posting this query for Splitting String using Oracle SQL 9i.Your blog is very useful, especially for a beginner like me :)

sap pp

Anonymous said...

Great query!

token listrik said...

Dear suresh, thanks for this query very usefull for splitting the text

Anonymous said...

It's very useful, Thanks :)

Copyright (c) All rights reserved. Presented by Suresh Vaishya