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 > Splitting A Record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-03-12, 14:58
madala99 madala99 is offline
Registered User
 
Join Date: Feb 2004
Posts: 16
Splitting A Record

Hi,
I have a table with 2 columns

Id,Loc
1,NJ@NY@PA@CA
2,CT
3,CA@VA



I want the output to be
1,nj
1,ny
1,pa
1,ca
2,ct
3,ca
3,va

Not sure how to split the data.@ is delimter in data.Record count can change and there can be N number of delimiters in data
Reply With Quote
  #2 (permalink)  
Old 02-03-12, 15:24
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Have you bothered to search, or would you like other people to do it for you? There's another thread on the subject on this same page.
Reply With Quote
  #3 (permalink)  
Old 02-03-12, 20:56
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Another point which you should keep in mind would be
to provide exact descriptions and sample data covering as many cases as possible for your requirements.

For example:
1) Your output sample were lower cases while input were upper cases.
2) All element length in your sample were 2.

If those were all possible cases, the following sample would be enough

Example 1: Up to 100 elements.
Note: this might not be your expected solution. But, returned exactly same result you wanted.
Code:
------------------------------ Commands Entered ------------------------------
WITH
  t(id , loc) AS (
VALUES
  ( 1 , 'NJ@NY@PA@CA' )
, ( 2 , 'CT'          )
, ( 3 , 'CA@VA'       )
)
SELECT VARCHAR(id) || ',' ||
       LOWER( SUBSTR(loc , k * 3 - 2 , 2) ) AS output
 FROM  t
 INNER JOIN (
       SELECT k1 + k2 AS k
        FROM (VALUES     1, 2, 3, 4, 5, 6, 7, 8, 9,10 ) k(k1)
           , (VALUES  0,10,20,30,40,50,60,70,80,90    ) k(k2)
       ) k
   ON  k <= LENGTH(loc) / 3 + 1
 ORDER BY
       id , k
;
------------------------------------------------------------------------------

OUTPUT        
--------------
1,nj          
1,ny          
1,pa          
1,ca          
2,ct          
3,ca          
3,va          

  7 record(s) selected.

Last edited by tonkuma; 02-03-12 at 21:06.
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