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

Update multiple positions with in a field


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

New User


Joined: 17 Mar 2009
Posts: 27
Location: Bangalore

PostPosted: Fri May 20, 2011 3:53 pm
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
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: 27
Location: Bangalore

PostPosted: Fri May 20, 2011 4:51 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Fri May 20, 2011 4:56 pm
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: 28
Location: germany

PostPosted: Fri May 20, 2011 5:01 pm
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: 6966
Location: porcelain throne

PostPosted: Fri May 20, 2011 5:21 pm
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
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: 27
Location: Bangalore

PostPosted: Fri May 20, 2011 8:35 pm
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Grouping by multiple headers DFSORT/ICETOOL 7
No new posts How to append a PS file into multiple... JCL & VSAM 3
Search our Forums:

Back to Top