Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Update multiple positions with in a field

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Vamshi Veludandi

New User


Joined: 17 Mar 2009
Posts: 23
Location: Bangalore

PostPosted: Fri May 20, 2011 3:53 pm    Post subject: Update multiple positions with in a field
Reply with quote

Hi, I have a requirement where in I need to update data at multiple positions of a varchar field. The field MEMGRP is of length 55 (basically PIC X(11) OCCURS 5 TIMES).

I need to replace SPACES with ZERO's.
If SUBSTR(MEMGRP,5,7) = ' ' then
set SUBSTR(MEMGRP,5,7) = '0000000'

If SUBSTR(MEMGRP,16,7) = ' ' then
set SUBSTR(MEMGRP,16,7) = '0000000'

If SUBSTR(MEMGRP,27,7) = ' ' then
set SUBSTR(MEMGRP,27,7) = '0000000'

If SUBSTR(MEMGRP,38,7) = ' ' then
set SUBSTR(MEMGRP,38,7) = '0000000'

If SUBSTR(MEMGRP,49,7) = ' ' then
set SUBSTR(MEMGRP,49,7) = '0000000'

I executed the following query and able to update for the first occurence successfully.
UPDATE MEMMSTR
SET MEMGRP = (SUBSTR(MEMGRP,1,4) || '0000000' || SUBSTR(MEMGRP,12,44)) where SUBSTR(MEMGRP,5,7) = ' '

But I need to update all the occurences in one shot.

I understand that this can be easily done using a cobol program. But I am actually looking to do it using a update query.

Please advice.

Vamshi.
Back to top
View user's profile Send private message

gylbharat

Active Member


Joined: 31 Jul 2009
Posts: 565
Location: Bangalore

PostPosted: Fri May 20, 2011 4:37 pm    Post subject:
Reply with quote

Write 5 such update queries.
Back to top
View user's profile Send private message
Vamshi Veludandi

New User


Joined: 17 Mar 2009
Posts: 23
Location: Bangalore

PostPosted: Fri May 20, 2011 4:51 pm    Post subject:
Reply with quote

gylbharat wrote:
Write 5 such update queries.


I have about 4 million records to update.
Is it wise to run the query 5 times

Vamshi
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10326
Location: italy

PostPosted: Fri May 20, 2011 4:56 pm    Post subject: Reply to: Update multiple positions with in a field
Reply with quote

if performance is an issue writing a cobol program should not be that difficult

or evaluate and unload / SORT / reload
Back to top
View user's profile Send private message
bauer

New User


Joined: 03 Mar 2009
Posts: 26
Location: germany

PostPosted: Fri May 20, 2011 5:01 pm    Post subject:
Reply with quote

Have a look to the translate function of DB2. This function can be used to convert one charcater to any other character.

Untested SQL

UPDATE MYTable
SET MyField = TRANSLATE(MyField,"X","Y") WHERE AnyCondition.


Hope this helps,
regards,
bauer
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6968
Location: porcelain throne

PostPosted: Fri May 20, 2011 5:21 pm    Post subject:
Reply with quote

unless the varchar COLUMN in all rows is max length,
you will need to include some length checks to prevent negative sqlcode returns.

translate or replace would be good options for this.

IMUO:

small varchar fields are really useless with DB2's datacompression.

varchar sql syntax requires (especially in this case) knowing the length of the column.

sounds as if you are trying to initialize these varchar columns,
if each varchar column is 55, a simple set of CASE statements could perform this in one shot.
but, if there are differing lengths, you end up with a set of convoluted CASE statements, and since I don't know how to code a NOP (no operation) for the ELSE clause in a CASE statement,
can't help you.

your reluctance to run multiple queries is silly,
by now you could have done it.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri May 20, 2011 7:13 pm    Post subject:
Reply with quote

Code:
update MEMMSTR
set MEMGRP =
substr(MEMGRP,1,4)||
case when substr(MEMGRP,5,7) = ' ' then '0000000' else substr(MEMGRP,5,7) end ||
substr(MEMGRP,12,4)||
case when substr(MEMGRP,16,7) = ' ' then '0000000' else substr(MEMGRP,16,7) end ||
substr(MEMGRP,23,4)||
case when substr(MEMGRP,27,7) = ' ' then '0000000' else substr(MEMGRP,27,7) end ||
substr(MEMGRP,34,4)||
case when substr(MEMGRP,38,7) = ' ' then '0000000' else substr(MEMGRP,38,7) end ||
substr(MEMGRP,45,4)||
case when substr(MEMGRP,49,7) = ' ' then '0000000' else substr(MEMGRP,49,7) end

where
   substr(MEMGRP,5,7) = ' '
or substr(MEMGRP,16,7) = ' '
or substr(MEMGRP,27,7) = ' '
or substr(MEMGRP,381,7) = ' '
or substr(MEMGRP,49,7) = ' '
Back to top
View user's profile Send private message
Vamshi Veludandi

New User


Joined: 17 Mar 2009
Posts: 23
Location: Bangalore

PostPosted: Fri May 20, 2011 8:35 pm    Post subject: Reply to: Update multiple positions with in a field
Reply with quote

GuyC,

Thanks a lot !!!

Your query worked icon_smile.gif perfectly.

Thank you all for your responses.

Got to learn something new today icon_smile.gif.

Vamshi.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Update table without cursor Ron Klop DB2 3 Wed Oct 11, 2017 1:06 pm
No new posts sort records based on length exceeds ... maxsubrat DFSORT/ICETOOL 7 Wed Oct 04, 2017 4:48 pm
No new posts Partial color change of a field in CI... waseem0424 CICS 5 Fri Sep 29, 2017 7:56 pm
No new posts ejck thru rexx to multiple jobs insid... Susanta All Other Mainframe Topics 5 Tue Sep 19, 2017 1:39 pm
No new posts PLI - printing from multiple programs Pedro PL/I & Assembler 8 Fri Sep 08, 2017 6:36 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us