Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, shivanshu.

Asked: June 29, 2016 - 9:13 am UTC

Last updated: June 30, 2016 - 3:25 am UTC

Version: Version 4.0.2.15

Viewed 1000+ times

You Asked

Hi,

I need some help to converting the string data from string to coulmns wise, i have the data in one column like :
#1#5#3#2#5#7
#5#3#5#4#3#2
#1#7#5#3#6
#3#5#2#1#5
#6#5#4#7#1#2

and i need to each numberic should be columns wise like this

COL_A COL_B COL_C COL_D COL_E COL_F
1 5 3 2 5 7
5 3 5 4 3 2
1 7 5 3 6
3 5 2 1 5
6 5 4 7 1 2

Thanks,
Shivanshu

and Connor said...


SQL> drop table t purge;

Table dropped.

SQL> create table t ( str varchar2(100));

Table created.

SQL>
SQL>
SQL> insert into t values ('#1#5#3#2#5#7');

1 row created.

SQL> insert into t values ('#5#3#5#4#3#2');

1 row created.

SQL> insert into t values ('#1#7#5#3#6');

1 row created.

SQL> insert into t values ('#3#5#2#1#5');

1 row created.

SQL> insert into t values ('#6#5#4#7#1#2');

1 row created.

SQL> with hash_pos as
  2    ( select
  3         str,
  4         instr(str,'#',1,1) h1,
  5         instr(str,'#',1,2) h2,
  6         instr(str,'#',1,3) h3,
  7         instr(str,'#',1,4) h4,
  8         instr(str,'#',1,5) h5,
  9         instr(str,'#',1,6) h6,
 10         instr(str,'#',1,7) h7,
 11         instr(str,'#',1,8) h8,
 12         instr(str,'#',1,9) h9,
 13         instr(str,'#',1,10) h10
 14      from ( select str||'#' str from t )
 15     )
 16  select
 17    substr(str,h1+1,h2-h1-1) s1,
 18    substr(str,h2+1,h3-h2-1) s3,
 19    substr(str,h3+1,h4-h3-1) s4,
 20    substr(str,h4+1,h5-h4-1) s5,
 21    substr(str,h5+1,h6-h5-1) s6,
 22    substr(str,h6+1,h7-h6-1) s7,
 23    substr(str,h7+1,h8-h7-1) s8,
 24    substr(str,h8+1,h9-h8-1) s9
 25  from hash_pos;

S1     S3     S4     S5     S6     S7     S8     S9
------ ------ ------ ------ ------ ------ ------ ------
1      5      3      2      5      7
5      3      5      4      3      2
1      7      5      3      6
3      5      2      1      5
6      5      4      7      1      2

5 rows selected.

SQL>


You can throw a "to_number" in there as well if you need.

Is this answer out of date? If it is, please let us know via a Comment