473,505 Members | 13,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

split a contents of a field of each record and store in another table

9 New Member
Hai everybody...!
I have a problem here .. Hope I can get help by answers..

I have a table A
ID DATA
1 aa;bb
2 aa;bb;cc
3 aa;cc
4 bb;cc

I wish to get a table B like

ID A B C
1 aa bb null
2 aa bb cc
3 aa null cc
4 null bb cc

I would like get table B a result by using DB2 UDF,,
wish have details of create function & how to call it ..
I am new to DB2.. waiting for Help..!

Thanks in advance...
Mar 19 '13 #1
12 2154
r035198x
13,262 MVP
You'd have to write your own string split function. Luckily someone has already done that and explained the process here:http://www.ibm.com/developerworks/da...03stolze1.html
Mar 19 '13 #2
ssmvijay
9 New Member
Thanks for your response..
I have tried that one earlier.. it throws [Error] - &;WHERE ordinal. SQLCODE=-7, SQLSTATE=42601, DRIVER=3.53.71

I am using DB2 Aqua Data Studio 8.0.8

so waiting for further clarification..
It would be very helpful ..
Thanks once again in advance..
Mar 19 '13 #3
r035198x
13,262 MVP
What code did you execute when you go that error?
Mar 19 '13 #4
ssmvijay
9 New Member
Here is the code I used...

Expand|Select|Wrap|Line Numbers
  1. 1)CREATE or REPLACE FUNCTION elemIdx (string CLOB(64K) ) 
  2. 2)RETURNS TABLE ( ordinal INTEGER, index INTEGER ) 
  3. 3)LANGUAGE SQL 
  4. 4)DETERMINISTIC 
  5. 5)NO EXTERNAL ACTION 
  6. 6)CONTAINS SQL 
  7. 7)RETURN WITH t(ordinal, index) AS 
  8. 8) ( VALUES ( 0, 0 ) 
  9. 9)  UNION ALL 
  10. 10) SELECT ordinal+1, COALESCE(NULLIF(LOCATE(';', 
  11. 11) string,index+1),0),LENGTH(string)+1) FROM t           
  12. 12) WHERE  ordinal < 10000 AND LOCATE(';', string, 
  13. 13)     index+1) <> 0 ) 
  14. 14)     SELECT ordinal, index  FROM   t 
  15. 15)     UNION ALL       
  16. 16)     SELECT MAX(ordinal)+1, LENGTH(string)+1 FROM   t 
In the above code I am not clear about the following...

[ CLOB(64K) ]
[ <10000 AND ]
[ <> ]

Thanks in advance..
Mar 19 '13 #5
r035198x
13,262 MVP
Don't just copy and paste code without looking at what you are executing. That is quite a dangerous thing to do.

1.) Don't execute it with those line numbers
2.) Read the tutorial to understand what the SQL is doing.
Mar 19 '13 #6
ssmvijay
9 New Member
Thanks..
I do executed without line numbers ..
just for understanding i added line number manually for posting reply...
as i mentioned early
[ CLOB(64K) ]
[ <10000 AND ]
[ <> ]

are not mentioned clearly in tutorial...
Thanks again..
Mar 19 '13 #7
r035198x
13,262 MVP
Most likely some of the characters are supposed to be less than (<) instead of &lt; and their code formatter didn't display them right so replace &lt; with < and &gt; with >
Mar 19 '13 #8
ssmvijay
9 New Member
yes you are right...!

http://www.ibm.com/developerworks/da...03stolze1.html

helps to get the seperate Stings from a group of String as input ...

i.e.s from 'aa;bb;cc' as input gives 'aa' 'bb' 'cc' in three records as output...

but still i am not getting how to get ...

table B
ID A B C
1 aa bb null
2 aa bb cc
3 aa null cc
4 null bb cc

as output from the input

table A
ID DATA
1 aa;bb
2 aa;bb;cc
3 aa;cc
4 bb;cc

i.e., how to pass each DATA in all records of table A as String (by looping) as input
& how to insert the result into the fields A B C of table B respectively..

Thanks Again in Advance...
Mar 20 '13 #9
r035198x
13,262 MVP
insert from a select (or from a group of selects)
Mar 20 '13 #10
ssmvijay
9 New Member
Thanks.. but still not getting how to insert with group of select...
I have the following...
[code - function returns a table]
Expand|Select|Wrap|Line Numbers
  1. CREATE or REPLACE FUNCTION elemIdx ( string VARCHAR(225) ) 
  2.    RETURNS TABLE ( ordinal INTEGER, index INTEGER ) 
  3.    LANGUAGE SQL 
  4.    DETERMINISTIC 
  5.    NO EXTERNAL ACTION 
  6.    CONTAINS SQL 
  7.    RETURN 
  8.       WITH t(ordinal, index) AS 
  9.          ( VALUES ( 0, 0 ) 
  10.            UNION ALL 
  11.            SELECT ordinal+1, COALESCE(NULLIF(LOCATE(';', string, index+1), 0),LENGTH(string)+1) FROM   t 
  12.                   WHERE  ordinal<1000 AND  LOCATE(';', string, index+1) <> 0 ) 
  13.       SELECT ordinal, index FROM   t 
  14.       UNION ALL 
  15.       SELECT MAX(ordinal)+1, LENGTH(string)+1 FROM t 
  16.  
  17.  
[code - query ]
Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM TABLE (elemIdx('abc; def; ghi; 123') ) AS t ORDER BY 1
  3.  
[result of above query - a table]
Expand|Select|Wrap|Line Numbers
  1.   ordinal  index
  2.     0       0
  3.     1       4
  4.     2       9
  5.     3       14
  6.     4       19
[result]

[code-function returns a table]
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE FUNCTION elements ( string VARCHAR(225) ) 
  2.    RETURNS TABLE ( elements VARCHAR(225) ) 
  3.    LANGUAGE SQL 
  4.    DETERMINISTIC 
  5.    NO EXTERNAL ACTION 
  6.    CONTAINS SQL 
  7.    RETURN 
  8.       WITH t(ordinal, index) AS 
  9.          ( SELECT ordinal, index FROM   TABLE ( elemIdx(string) ) AS x ) 
  10.       SELECT SUBSTR(string, t1.index+1, t2.index - t1.index - 1) 
  11.             FROM   t AS t1 JOIN t AS t2 ON ( t2.ordinal = t1.ordinal+1 ) 
  12.  
  13.  
[code- query that returns a table again]
Expand|Select|Wrap|Line Numbers
  1. SELECT VARCHAR(elem, 100) 
  2. FROM   TABLE ( elements('abc; def; ghi; 123') ) AS t(elem)
  3.  
  4.  
[result of above query - a table]
Expand|Select|Wrap|Line Numbers
  1.   1
  2.  abc
  3.  def
  4.  ghi
  5.  123
  6.  
[result]

i donot have any clear idea about
how to proceed further....

Help me how to proceed ... Thanks in Advance...
Mar 20 '13 #11
r035198x
13,262 MVP
insert into your new table values(colA, colB)
values
(select from the table with the concatenated values and split the values and get the first split record, select from the table with the concatenated values and split the values and get the second split record)
Mar 20 '13 #12
ssmvijay
9 New Member
Thanks...
I am not getting any progress...
as I am getting close to deadline,could you help me with queries using above functions..

source table is
Table_name as source
ID DATA
1 aa;bb
2 aa;bb;cc
3 aa;cc
4 bb;cc

and destination table is

Table_name as destination (ID is auto generated)
ID A B C
- - - -

expected result is
Table_name: destination
ID A B C
1 aa bb null
2 aa bb cc
3 aa null cc
4 null bb cc

Thanks in Advance..
Mar 20 '13 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1293
by: sans_spam | last post by:
I'm writing a classic ASP application that records all logging of user logins on our support site. The logging is a rolling window of how many people have logged in for a given month, i.e. tracked...
1
1831
by: Joe Bloggs | last post by:
Hello, I have a form linking two tables but they have one field that is shared called "device" I want to allow data input into one of the fields and hide the other (so I need it automatically...
1
1425
by: SheldonMopes | last post by:
I'm not sure how to go about this: I have a form bound to tableA. I would like to be able to use the AfterUpdate event of textbox1 to write the contents (after some manipulation) to TableB. How...
10
2601
by: jaYPee | last post by:
as of now i am using a stored procedure from my sql server 2000 to insert record from another table. what i need now is on how can i insert record by not using the stored procedure and insert it...
2
2469
by: Prabu Subroto | last post by:
Dear my friends... I created a table (named : sven1). I want to populate this table with the record from another table (named : appoinment). but I don't know how to formulate the sql query. ...
1
4282
by: travismorien | last post by:
I have four tables of different "entities". One table contains information for "people", one for "trusts", one for "companies" and one for "self managed super funds". Each type of entity has an...
2
2061
by: Deano | last post by:
Problem is that there are lots and lots of fields in an employee record. I specify a form control as the criterion for one of those fields which is the value of the primary key for that record. ...
2
1545
by: MLH | last post by:
DELETE tblPreliminaryVINs.* FROM tblPreliminaryVINs INNER JOIN tblVehicleJobs ON tblPreliminaryVINs.PVIN = tblVehicleJobs.SerialNum; The above SQL does not work for me. I get an error I cannot...
3
2865
by: webcat | last post by:
Hi I need to UPDATE data into a table mainData which contains many fields - one is CODE and one is DESCRIPTION another table DESCRIP is a lookup - it also contains the same fields, but...
2
1400
by: lee weaver | last post by:
I have a table for employees and a table that shows which containers they have access to and a table of all containers. What i need to do is when adding a new employee i need to add a record to...
0
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.