Programming Languages on Power

Power Programming Languages

IBM Power, including the AIX, IBM i, and Linux operating systems, support a wide range of programming languages, catering to both traditional enterprise applications and modern development needs.


#Power


#Servers
#Programminglanguages
 View Only
Expand all | Collapse all

How to convert an integer into a string while retaining the leading zeros?

  • 1.  How to convert an integer into a string while retaining the leading zeros?

    Posted 09/19/24 10:04 AM

    I have an SQL service in IBM i which returns an integer.  I need to convert it to a six digit string with the leading zeros.

    Using the number 1037 (which the service is currently returning) I came up with this:

    values right('000000' concat cast(cast(1037 as intas varchar (6)), 6);

    So in production this would be

    values right('000000' concat cast(myinteger as varchar (6)), 6);

    Is there a more standard practices way?



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    260-599-3160
    ------------------------------

    #SQL


  • 2.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/19/24 10:10 AM

    I'd rather use the DIGITS function, like:

     

    values right(digits(cast(1037 as int)), 6);

     

    If your variable has the right size, you do not even need the "right".

     

    best regards

     

    ANTON GOMBKÖTÖ

    Softwareentwickler

     

    image001.png@01D20E98.B27FBF20

    CPB SOFTWARE (AUSTRIA) GMBH
    Campus Viertel Zwei

    Vorgartenstraße 206c, A-1020 Wien

     

    T: +43 1 427 01-204
    Anton.Gombkoetoe@cpb-software.com
    www.cpb-software.com

    FN 174113z, Handelsgericht Wien

     

     

    This message is intended for the named addressee only and shall not create any legal obligation upon CPB Software AG or any of its affiliates or subsidiaries unless independently confirmed in writing. Any disclosure or dissemination to third parties is strictly prohibited.

     






  • 3.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/19/24 10:43 AM

    DIGITS was what I couldn't remember.  Thank you.

    Had to use the RIGHT when trying to stuff an INT into a CHAR(6) required by RLSJOBSCDE  JOB(...) ENTRYNBR(here)

    Otherwise I was getting all zeros.



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    260-599-3160
    ------------------------------



  • 4.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 08:16 AM

    just curious - do you have jobs in the job scheduler with the same name ? If not, the entry number isn't required if you accept the default as *ONLY. Also, and just in case you are not aware - this sql service can be used to work with your scheduled jobs qsys2.scheduled_job_info and consumed into your program if that makes life easier for you...(now or in the future). Thanks



    ------------------------------
    Rich Malloy
    ------------------------------



  • 5.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 08:24 AM

    Rich:

    Part 1:

    Job        
    BRMMAINT   
    BRMRECOVER 
    BRMREORG   
    BRMSSTATUS 

    Part 2:

    ...

              DECLARE BRM CURSOR FOR
                select SCHEDULED_JOB_NAME, SCHEDULED_JOB_ENTRY_NUMBER
                FROM QSYS2.SCHEDULED_JOB_INFO
                WHERE SCHEDULED_JOB_NAME LIKE 'BRM%'
                  AND STATUS = 'HELD';

    ...

              -- WRKJOBSCDE BRM*
              -- Release any which are held
              SET END_TABLE = 0;
              OPEN BRM;
              FETCH BRM INTO JOBNAME, JOB_ENTRY_NBR;
              WHILE END_TABLE = 0 DO
                 SET JOB_ENTRY_NBR_CHAR = LPAD(JOB_ENTRY_NBR, 6'0');
                 SET COMMAND = 'RLSJOBSCDE JOB(' concat JOBNAME concat
                    ') ENTRYNBR(' concat JOB_ENTRY_NBR_CHAR concat ')';
                 CALL QSYS2.QCMDEXC(COMMAND);
                 FETCH BRM INTO JOBNAME, JOB_ENTRY_NBR;
               END WHILE;
               CLOSE BRM;

    ...



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    260-599-3160
    ------------------------------



  • 6.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 09:14 AM

    Based on the above, I don't believe you need entry number since you don't have duplicate job names. Since the job name is unique, you should be able to just use *ONLY (which is the default) ....I can see if you had duplicate job names then yes, the entry number is required but that doesn't seem to be the case. I was just curious as to why you were needing the entry number....glad its working for you though. Rich



    ------------------------------
    Rich Malloy
    ------------------------------



  • 7.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/23/24 07:56 AM

    I may not need it, at this time.  I have over a dozen differing lpars of IBM i and it's not unreasonable that there may be duplicates at some time.

    Besides, I have the logic working now and it can be duplicated, if needed, for other such entries.



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    260-599-3160
    ------------------------------



  • 8.  RE: How to convert an integer into a string while retaining the leading zeros?
    Best Answer

    Posted 09/20/24 01:46 AM

    This would be an easier solution:

    Values(LPAD(Cast(1037 as integer), 6'0'));



    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Birgitta Hauser - Modernization-Education-Consulting on IBM i (selfemployed)
    Kaufering
    +49 170 5269964
    ------------------------------



  • 9.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 07:37 AM

    This works great.  In production now.  Thank you.



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    260-599-3160
    ------------------------------



  • 10.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 04:33 AM

    use straight LPAD , doesn't even require casting, it accepts multiple types.



    ------------------------------
    --ft
    ------------------------------



  • 11.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 05:47 AM
    Edited by Birgitta Hauser 09/20/24 05:48 AM

    ... CAST was necessary to convert the number into an integer (which can have up to 10 digits) to prove it can be converted into 6 digit long result.



    ------------------------------
    Birgitta Hauser
    Database and Software Engineer
    Birgitta Hauser - Modernization-Education-Consulting on IBM i (selfemployed)
    Kaufering
    +49 170 5269964
    ------------------------------



  • 12.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 08:24 AM

    varchar format should also work.

    values VARCHAR_FORMAT(1037'099999999999999999') ;

    I usually use to add a comma in large numbers i.e.

    VARCHAR_FORMAT(LargeNumber, '999G999G999G999G999G999')



    ------------------------------
    Bryan Dietz
    ------------------------------



  • 13.  RE: How to convert an integer into a string while retaining the leading zeros?

    Posted 09/20/24 08:29 AM

    VARCHAR_FORMAT has it's uses but, damn it's not very self explanatory.  No, I'll stick with something else unless necessary.



    ------------------------------
    Robert Berendt IBMChampion
    Business Systems Analyst, Lead
    Dekko
    Fort Wayne
    260-599-3160
    ------------------------------