Tutorial66.com
@Share Application Development Skill.



Tutorial is tutorial...66 is My Country Code Up to 10 Years EXP for Application Development Role in Thailand.
collapse

Tutorial Category



Topic: PLSQL split words with delimiter by only one SQL Command  (Read 227 times)


My Development skill
Java JSP Servlet EJB
Spring Framework
Hibernate Framework
iReport Jasper Report
Apache CXF
PHP Codigniter
Oracle PLSQL
Unix Shell Script
PLSQL split words with delimiter by only one SQL Command
« on: October 26, 2011, 12:30:39 AM »
PLSQL split words with delimiter by only one SQL Command
Often, when you need to substr in to rows of table or in to array  you tend to think of function substr,instr.
This example will show you for only one SQL command to solve this case

Example 1 Split words in to rows of table with SQL query
Code: [Select]
SELECT substr(main_string, position_from + 1, position_to - position_from - 1)
FROM (SELECT main_string,
decode(rownum - 1, 0, 0, instr(main_string, ',', 1, rownum - 1)) position_from,
instr(main_string, ',', 1, rownum) position_to
FROM (SELECT 'PLSQL1,PLSQL2,PLSQL3,PLSQL4,' main_string
FROM dual)
CONNECT BY LEVEL <= length(main_string))
WHERE position_to > 0
Note for this case
 in the end of str_string need end delimiter

Quote
FROM (SELECT 'PLSQL1,PLSQL2,PLSQL3,PLSQL4,' main_string

If you need to count the word you can try that
Code: [Select]
select count(*) from (
SELECT substr(main_string, position_from + 1, position_to - position_from - 1)
FROM (SELECT main_string,
decode(rownum - 1, 0, 0, instr(main_string, ',', 1, rownum - 1)) position_from,
instr(main_string, ',', 1, rownum) position_to
FROM (SELECT 'PLSQL1,PLSQL2,PLSQL3,PLSQL4,' main_string
FROM dual)
CONNECT BY LEVEL <= length(main_string))
WHERE position_to > 0
)
8) 8)




SimplePortal 2.3.3 © 2008-2010, SimplePortal