If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > split function in sql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-03, 17:38
huskers huskers is offline
Registered User
 
Join Date: May 2002
Posts: 15
split function in sql

Hi,

I have a column in the tables as follows:

Table X
A
12345,1234123,123
34523,2312,232
75657,34334,123

Table Y
B C D
12345 1234123 asd
34523 2312 dafd
75657 34334 sda

I need to split the data that is comma delimited in Table X and join it with column B and C in Table Y. Is there a way i can do it in sql.

Ex: 1st row, i need to split to get: 12345 1234123 123
now i need to use 12345 and 1234123 to join with column in other table.

I can not use substr() function as the length might vary.
Reply With Quote
  #2 (permalink)  
Old 12-16-03, 19:32
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: split function in sql

I think you can achieve split with a combination of LOCATE, POSSTR, SUBSTR Functions



Quote:
Originally posted by huskers
Hi,

I have a column in the tables as follows:

Table X
A
12345,1234123,123
34523,2312,232
75657,34334,123

Table Y
B C D
12345 1234123 asd
34523 2312 dafd
75657 34334 sda

I need to split the data that is comma delimited in Table X and join it with column B and C in Table Y. Is there a way i can do it in sql.

Ex: 1st row, i need to split to get: 12345 1234123 123
now i need to use 12345 and 1234123 to join with column in other table.

I can not use substr() function as the length might vary.
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 12-17-03, 10:32
huskers huskers is offline
Registered User
 
Join Date: May 2002
Posts: 15
Thanks for the reply, it helped....
Reply With Quote
  #4 (permalink)  
Old 12-17-03, 10:58
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Can you share with us how you did it, please ?

Cheers
Sathyaram

Quote:
Originally posted by huskers
Thanks for the reply, it helped....
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 12-17-03, 11:11
huskers huskers is offline
Registered User
 
Join Date: May 2002
Posts: 15
The first part of the data that i am using contains a unix time so this made things easy for me. The second part keeps varying. I just need to pasrse out the first and second parts of comma delimited data.

Table X:
A
1071503689,58500,4029631200
1071503689,5850013,4029631200

so i used the following query:

select A,substr(A, 1, 10), substr(A,12,posstr(substr(A, 12, locate(',',A,12)),',')-1) from X

Let me know if we can improve this further.

Thanks
Reply With Quote
  #6 (permalink)  
Old 12-17-03, 11:16
huskers huskers is offline
Registered User
 
Join Date: May 2002
Posts: 15
this is more generalized:

select A,substr(A,1,locate(',',A)-1), substr(A,12,posstr(substr(A, 12, locate(',',A,12)),',')-1) from X
Reply With Quote
  #7 (permalink)  
Old 12-17-03, 11:47
huskers huskers is offline
Registered User
 
Join Date: May 2002
Posts: 15
I was able to parse the 1st and 2nd parts of the string using the following query but am unable to get the 3rd part.

SELECT
A,
substr(A,1,locate(',',A)-1),
substr(A,locate(',',A)+1,posstr(substr(A, locate(',',A)+1, locate(',',A,locate(',',A)+1)),',')-1)
FROM X


Any advice.
Reply With Quote
  #8 (permalink)  
Old 12-18-03, 11:23
dmmac dmmac is offline
Registered User
 
Join Date: Aug 2003
Location: Massachusetts, USA
Posts: 106
Here is another possibility. Seems to work (tested with the data you provided):

SELECT
A,
substr(A,1,locate(',',A)-1),
substr(substr(A,locate(',',A,locate(',',A))+1),1,p osstr(substr(A,locate(',',A,locate(',',A))+1),',')-1),
substr(A,locate(',',A,locate(',',A)+1)+1)
FROM X

Quote:
Originally posted by huskers
I was able to parse the 1st and 2nd parts of the string using the following query but am unable to get the 3rd part.

SELECT
A,
substr(A,1,locate(',',A)-1),
substr(A,locate(',',A)+1,posstr(substr(A, locate(',',A)+1, locate(',',A,locate(',',A)+1)),',')-1)
FROM X


Any advice.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On