MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Update leading spaces to zeroes VARCHAR

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Oct 01, 2010 12:20 pm    Post subject: Update leading spaces to zeroes VARCHAR Reply with quote

I searched and did not find anything that would help.

I need to update all leading spaces in a varchar column to zeroes.

I tried the following:

set text11 = SUBSTR(text11,01,03) = '000'

sorry I tired more but we lost power and I lost my samples Embarassed

I also tried a LEFT function

Any help will be appreciated.
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Oct 01, 2010 1:09 pm    Post subject: Reply with quote

I got it Very Happy

set text11 = '000' || (SUBSTR(text11,04,30))
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Fri Oct 01, 2010 1:12 pm    Post subject: Reply with quote

What happens if there are other than 3 leading zeros?
_________________
All the best,

di
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Oct 01, 2010 1:39 pm    Post subject: Reply with quote

papadi,
I need to update a 9 position number. The leading zeroes were not loaded in the table. I will need to update multiply times depending on the number.
This is a one time fix.


'0' || (SUBSTR(text11,02,30))
'00' || (SUBSTR(text11,03,30))
'000'|| (SUBSTR(text11,04,30))
'0000'|| (SUBSTR(text11,05,30))
ect.
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12382
Topics: 75
Location: San Jose

PostPosted: Fri Oct 01, 2010 2:21 pm    Post subject: Reply with quote

Nascar9,

Here is a generic sql (untested) which would give you the desired results. The basic idea is to get the length of the leading spaces length using LTRIM and RTRIM functions and subtracting the lengths.

Code:

UPDATE table                                             
   SET Var_col = REPLACE(SUBSTR(Var_col,1,                           
                         LENGTH(RTRIM(Var_col)) -                     
                         LENGTH(LTRIM(RTRIM(Var_col)))),' ','0') || 
                         LTRIM(Var_col)                             
  ;


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Oct 01, 2010 2:31 pm    Post subject: Reply with quote

Thanks Kolusu,
I will need to test your query. This is what I ran

Code:


set text13   =   CASE
                  WHEN  (SUBSTR(text13,01,8)) = '        '      THEN  '00000000' || (SUBSTR(text13,09,40))       
                  WHEN  (SUBSTR(text13,01,7)) = '       '      THEN  '0000000' || (SUBSTR(text13,08,40))     
                  WHEN  (SUBSTR(text13,01,6)) = '      '      THEN  '000000' || (SUBSTR(text13,07,40))     
                  WHEN  (SUBSTR(text13,01,5)) = '     '      THEN  '00000' || (SUBSTR(text13,06,40))
                  WHEN  (SUBSTR(text13,01,4)) = '    '       THEN  '0000' || (SUBSTR(text13,05,40))
                  WHEN  (SUBSTR(text13,01,3)) = '   '       THEN  '000' || (SUBSTR(text13,04,40)) 
                  WHEN  (SUBSTR(text13,01,2)) = '  '       THEN  '00' || (SUBSTR(text13,03,40)) 
                   ELSE   TEXT13
                          END

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Oct 05, 2010 6:42 pm    Post subject: Reply with quote

I finally got around to testing Kolusu's code.

It works! It is also alot less code then my solution.

Thanks again!
Code:

  SET text11 = REPLACE(SUBSTR(text11,1,                           
                         LENGTH(RTRIM(text11))-                     
                         LENGTH(LTRIM(RTRIM(text11)))),' ','0') || 
                         LTRIM(text11)       

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group