ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Appending leading zeros to numbers in a alpha numeric field

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Appending leading zeros to numbers in a alpha numeric field

    Hi everyone,

    I have a 10 digit alpha numeric field in which I want to pad leading zeros only for numeric values which are less than 10 in the field, not the alphanumeric field, and move it to another variable . See below some examples. How do I do it?

    Appreciate help.

    Thanks in advance

    Surkum

    ex if the value in the field = '0112109', then the result value should be '0000112109' (in this case 3 leading zeros should be added to the original 7 digit long
    numeric value
    if the value in the field = '0504324A' then do nothing and the value should be as it is '0504324A'

  • #2
    Re: Appending leading zeros to numbers in a alpha numeric field

    Try:
    Code:
        If %Check('1234567890': YourVar) = *Zeros;
           EvalR YourVar = '0000000000' + %Trim(YourVar);
        EndIf;
    Birgitta
    Last edited by B.Hauser; January 15, 2014, 09:57 AM.

    Comment


    • #3
      Re: Appending leading zeros to numbers in a alpha numeric field

      Birgitta's answer is less work definitely, but this works too. It uses a helper field(field2) which is Zoned(10,0). Field1 is the starting results, and you can probably figure out what end_result is. The only thing that this offers is that there is no hard coding, but it is not like they are going to be adding more numbers anytime soon.

      Code:
               monitor;
                  field2 = %dec(field1:10:0);
                  end_result = %editc(field2:'X');
               On-Error;
                  end_result = field1;
               EndMon;

      Comment


      • #4
        Re: Appending leading zeros to numbers in a alpha numeric field

        Birgitta,

        Thanks for your reply. I tried your suggestion but I am getting all blanks in the result field. Is this because the field that I am reading from is left justified. I was debugging the program and the variable the program is reading from has the value like this '0112109 ' Can this be the reason why I am getting everything with blank.

        Thanks

        Surkum

        Comment


        • #5
          Re: Appending leading zeros to numbers in a alpha numeric field

          Hi,

          Code:
          d yourvar         s             10a    
          d newvar          s             10a    
           /free                                 
                                                 
            yourvar = '0112109';                                              
            if %Check('1234567890': %trimr(YourVar)) = *Zeros;                
              newvar = '0000000000';                                          
              %subst(newvar:11-%len(%trimr(yourvar)):%len(%trimr(yourvar))) = 
               %subst(yourvar:1:%len(%trimr(yourvar)));                       
              dsply newvar;                                                   
            endif;
          PS

          Birgittas example dosn't work for several reasons

          Yourvar initial also contains blanks so the if statement will fail
          YourVar = '0000000000' + Yourvar; will result in 00000000000112109, since the field is 10 char the real result will be 0000000000
          Last edited by Henrik Rutzou; January 15, 2014, 08:27 AM.

          Comment


          • #6
            Re: Appending leading zeros to numbers in a alpha numeric field

            Hi,

            here is the code that also handles preceding blanks ...

            Code:
              yourvar = ' 0112109';                                           
              if %Check('1234567890': %trim(YourVar)) = *Zeros;               
                newvar = '0000000000';                                        
                %subst(newvar:11-%len(%trim(yourvar)):%len(%trim(yourvar))) = 
                 %subst(%trim(yourvar):1:%len(%trim(yourvar)));               
                dsply newvar;                                                 
              endif;

            Comment


            • #7
              Re: Appending leading zeros to numbers in a alpha numeric field

              Originally posted by Henrik Rutzou View Post
              YourVar = '0000000000' + Yourvar; will result in 00000000000112109, since the field is 10 char the real result will be 0000000000
              Birgitta used EVALR, not EVAL.

              Comment


              • #8
                Re: Appending leading zeros to numbers in a alpha numeric field

                Birgitta's code will work if a blank is added to the %check. Note that she is using evalR for the assignment. Here code was missing the % in front of trim. Here is the corrected that I tested.

                Code:
                If %Check('1234567890 ': YourVar) = *Zeros;        
                   EvalR YourVar = '0000000000' + %Trim(YourVar);  
                EndIf;

                Comment


                • #9
                  Re: Appending leading zeros to numbers in a alpha numeric field

                  Sorry

                  I first noticed it now

                  Comment


                  • #10
                    Re: Appending leading zeros to numbers in a alpha numeric field

                    Thanks everyone for your suggestion. I tried the suggestion by Scott by modifying Birgitta's code and it worked. Appreciate all your help. I love this forum! You guys rock

                    Comment

                    Working...
                    X