IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

insert the same records but chancing a specific value


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
danylele74

New User


Joined: 03 Jul 2014
Posts: 28
Location: Italy

PostPosted: Mon May 02, 2016 7:22 pm
Reply with quote

Hi all,

i have this problem:

Code:

Table A.

 COl 1   
-----------
yyKKK    cc   
KKK aa ssss 
KKKqqqjj
KKKffff1122
yy   KKKWWW   
1234KKKcc   
a123f tttKKK 


I must insert the same records but chancing the KKK value in an another value (i.e ZZZ)


Table A (result).


Code:
 COl 1   
-----------
yyKKK    cc   
KKK aa ssss 
KKKqqqjj
KKKffff1122
yy   KKKWWW   
1234KKKcc   
a123f tttKKK

yyZZZ    cc   
ZZZ aa ssss 
ZZZqqqjj
ZZZffff1122
yy   ZZZWWW   
1234ZZZcc   
a123f tttZZZ



Can I do this with a query? If yes, how?

thank you.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3051
Location: NYC,USA

PostPosted: Mon May 02, 2016 10:45 pm
Reply with quote

Code:
      update table a
      set a.col1 =
           ( select replace(b.col1,'KKK','ZZZ') from table b
             where a.key = b.key
           )
Back to top
View user's profile Send private message
danylele74

New User


Joined: 03 Jul 2014
Posts: 28
Location: Italy

PostPosted: Mon May 02, 2016 11:57 pm
Reply with quote

I must INSERT the new records not update the key field.

Take a look of the result (last screenshot)

bye
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3051
Location: NYC,USA

PostPosted: Tue May 03, 2016 12:32 am
Reply with quote

look here

Code:
 INSERT INTO table a
     SELECT replace(b.col1,'KKK','ZZZ')   
                 where   FROM table b
                 LOCATE('KKK', col1) <> 0

Code:
LOCATE('KKK', col1) <> 0
is just to avoid key violation as you say above.
Back to top
View user's profile Send private message
danylele74

New User


Joined: 03 Jul 2014
Posts: 28
Location: Italy

PostPosted: Tue May 03, 2016 6:53 pm
Reply with quote

This is my query
Code:

SELECT REPLACE(col1,'KKK','ZZZ'))
  from db2.TABLE   
 where bank = 'aa'


Total records: 600


Code:
SELECT COUNT(*)
  from db2.TABLE   
 where bank = 'aa'
     and col1 like '%KKK%'



Total records: 200

Why this difference?

Thanks
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue May 03, 2016 7:15 pm
Reply with quote

Because for the first query you are pulling all records with bank = 'aa' and the second query you restrict the pull to only those records with bank='aa' and that have coll containing 'KKK' somewhere?
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 446
Location: USA

PostPosted: Tue May 03, 2016 7:53 pm
Reply with quote

Just to add to what Robert said:
In the first query, You are fetching 600 rows but doing a replace for only 200 of them. Count (first query Total Records) is showing how many rows are fetched and it only depends on the condition bank='aa'.

.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Join multiple records using splice DFSORT/ICETOOL 5
No new posts EZT program to build a flat file with... All Other Mainframe Topics 9
No new posts JCL sortcard to print only the records DFSORT/ICETOOL 11
Search our Forums:

Back to Top