ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Right Aligned in rpgle

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

  • Right Aligned in rpgle

    Hi all,

    I need to extract the current hour and minute, and store it in a variable to make a comparison on a sql query.
    I have done the following
    Code:
    EDITime = %int(%char(CurrentHr - 1) + %trim(%char(CurrentMn)));
    this gives the value that I need but right aligned, which is not good because the value I am comparing it to is left aligned, and won't make proper comparisons. Is there a better way to do this?
    I tried variable * 100 i.e. EDITime = EDITime * 100, and it will give the correct value but it is still not making the comparison that I need, so I am guessing it is not the correct way to do it? (also it is not very intuitive).

    the field I need to compare it to is a zoned length 6, so I declared EDITime also as zoned length 6, (DCL-S EDITime zoned(6:0) inz

  • #2
    %Char is a bad choice as it will drop leading zeros. So if CurrentHr and CurrentMn were both 9 then the resulting Int would be 89. What you want of course is 809. So you should really use %EditC with the edit code 'X' - that would preserve the leading zero. P.S. You could drop the trim from the %Char as it has no purpose.
    EDITime = %int(%char(CurrentHr - 1) +%EditC(CurrentMn: 'X'))); Give that try.

    Comment


    • #3
      Why are you converting these to character with %char and then converting back to numbers with %int? If these fields represent a time, why are you not using the time data type? What is the desired result? If CurrentHr=5 and CurrentMn=1, do you really want (as you've coded) "40" as the result? Jon assumes you wanted "400" as the result, which makes a bit more sense -- but, still, why not keep them in separate fields?

      Assuming that is what you want... just do (CurrentHr - 1) * 100 + CurrentMn. No need to convert to/from character.

      Comment


      • #4
        I'd just use a data structure.
        Code:
        DCL-DS YourDS    Inz;
          EditTime Zoned(6);
          Hour     Zoned(2)   Overlay(Time);
          Minute   Zoned(2)   Overlay(Time: *Next);
          Second   Zoned(2)   Overlay(Time: *Next);
        End-DS; 
        
        Hour = CurrentHr - 1;
        Minute = CurrentMN;
        Birgitta

        Comment


        • #5
          Originally posted by JonBoy View Post
          %Char is a bad choice as it will drop leading zeros. So if CurrentHr and CurrentMn were both 9 then the resulting Int would be 89. What you want of course is 809. So you should really use %EditC with the edit code 'X' - that would preserve the leading zero. P.S. You could drop the trim from the %Char as it has no purpose.
          EDITime = %int(%char(CurrentHr - 1) +%EditC(CurrentMn: 'X'))); Give that try.
          doing the above code gives EDITIME = 000750, I think I need 075000 instead...

          I need to make comparison for this field. They are a zoned type length 6 according to the dspffd 'tablename'
          Code:
          Mail
          Time
          
          114000
          104000
          165000
          101415
          101415
          114000
          162508





          Comment


          • #6
            Originally posted by Scott Klement View Post
            Why are you converting these to character with %char and then converting back to numbers with %int? If these fields represent a time, why are you not using the time data type? What is the desired result? If CurrentHr=5 and CurrentMn=1, do you really want (as you've coded) "40" as the result? Jon assumes you wanted "400" as the result, which makes a bit more sense -- but, still, why not keep them in separate fields?

            Assuming that is what you want... just do (CurrentHr - 1) * 100 + CurrentMn. No need to convert to/from character.
            that would give EDITIME = 000803. but I need 080300, so I guess I could multiply the variable by 100 again that would give me what I need

            Comment


            • #7
              so after taking all your advices into consideration, I got what I want, like the following:

              as stated above, the field EMTIM is a zoned (6:0) which is the field I need to compare my variable EDITime to.
              Code:
              DCL-S EDITime      zoned(6:0) inz;
              DCL-S CurrentHr    packed(2:0) inz;
              DCL-S CurrentMn    packed(2:0) inz;
              DCL-S CurrentSc    packed(2:0) inz;
              
              CurrentHr = %subdt(%time():*hours);  
              CurrentMn = %subdt(%time():*Minutes);
              CurrentSc = %subdt(%time():*Seconds);
              
              EDITime = %int(%Editc(CurrentHr:'X') + %Editc(CurrentMn:'X')     //this gives me 091515 if the current time is 9:15:15
              %Editc(CurrentSc:'X'));                                     
              
              Exec SQL
                 //stuff here
                 where....
                 emtim <= :EDITime
                 ...
                 ...;
              and thru debug, I see that :EDITime is the same (091515) but the query doesnt return anything. if I input the actual value 091515 in place of :EDITime e.g. emtim <= 091515, it returns records that I need to see.
              I am not seeing the problem here...please help!

              Comment


              • #8
                You need the current time in the format HHMMSS?
                If so try the following statement to get the numeric current time:
                Code:
                EDitTime = %Dec(%Time(): *HMS);
                The EMTIM column contains numeric data for a time in the format HHMMSS?

                Birgitta

                Comment


                • #9
                  Code:
                  Exec SQL
                  //stuff here
                  where....
                  emtim <= :EDITime
                  ...
                  Is that the entire WHERE clause? With the entire WHERE clause, can you show us a row that should have been selected?

                  It's almost impossible to say why something didn't without seeing all relevant parts.
                  Tom

                  There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

                  Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

                  Comment


                  • #10
                    It is dangerous to use %TIME three times to get the various parts of a single time. If the time is 11:59:59 when you get the hours, and 12:00:00 when you get the minutes and seconds, then EDITime would be 11:00:00, almost one hour too early. It would be better to get %TIME() once in a TIME variable, and then get the various parts from that variable.

                    (It would be even better to do what Birgitta suggested, since that would eliminate the need for any temporary variables.)

                    Comment

                    Working...
                    X