ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Convert number CYYMM into a date

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

  • Convert number CYYMM into a date

    HI,

    How can I convert my period number 5 0 = 11501 (CYYMM) into a date (without days) ?

  • #2
    Re: Convert number CYYMM into a date

    Use the API QWCCVTDT. To just get the CYYMM you just need to trim the remainder from the returned result.

    Chris...

    Comment


    • #3
      Re: Convert number CYYMM into a date

      how can I use API QWCCVTDT ? any examples?

      Comment


      • #4
        Re: Convert number CYYMM into a date

        The problem you will encounter is the lack of a day value, 00 will not work so you need to add 01 to your input value to make it work as you do not need the day in the returned date you can just trim it off. Here is a quick and dirty sample in C, you should be able to convert to any language you want.

        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        #include <qusec.h>
        #include <qwccvtdt.h>
        
        typedef struct EC_x {
                       Qus_EC_t EC;
                       char Exception_Data[48];
                       }EC_t;
        
        int main(int argc,char *argv[])  {
        char Buf[18];
        EC_t Error_Code;
        
        Error_Code.EC.Bytes_Provided = sizeof(Error_Code);
        
        QWCCVTDT("*YMD      ",
                 "1150101000000000",
                 "*YYMD     ",
                 Buf,
                 &Error_Code);
        printf("%.18s\n",Buf);
        exit(0);
        }

        Comment


        • #5
          Re: Convert number CYYMM into a date

          You say you want to convert your CYYMM to a "date without days". Isn't a CYYMM value already a date without days? What is the expected output?

          Comment


          • #6
            Re: Convert number CYYMM into a date

            If you need a date you have to define a day, for example the first day within the month otherwise it cannot be converted into a date.
            If you need to convert it with RPG, just multiply your numeric date with 100 and add 1 (first day in month) and then use the built-in-function %Date to convert it into a real date:

            Code:
              RealDate = %Date(YourNumdate * 100 + 1: *CYMD);
            With SQL it is a little more complex, but you may use one of the following statements:

            Code:
            Select Date(Digits(Dec(YourDate50 * 100000000 + 1000000) + 19000000000000, 14, 0),
                     Date(Digits(Dec(YourDate50 + 190000, 6, 0)) concat '01000000')
            From yourTable
            Birgitta

            Comment


            • #7
              Re: Convert number CYYMM into a date

              Scott,

              I think he has a number not a date value ?
              period number 5 0 = 11501
              So I think it is going to be a Packed Decimal 5,0?? In C it is stored in a character field but should be converted to character string before being passed to the API. He also needs to add a day number to use the API (does not accept 00 or ' ') so I suggested 01 for the first of the month, but he should be able to get any date format he needs once he does that and then format it however he wants.

              Chris...

              Comment


              • #8
                Re: Convert number CYYMM into a date

                My thought was the same as Scott's... he already has a date without days. If he wants an actual date field, then it has to have days. As some have suggested, he can use 01 as the days. Then he can store that date as a date field and later ignore the days portion I guess. But I wonder what the point is...?

                Comment


                • #9
                  Re: Convert number CYYMM into a date

                  Originally posted by chris_hird View Post
                  Scott,

                  I think he has a number not a date value ?
                  Chris, it's not clear to me what he's looking for. You say that what he has now is a number rather than a 'date value'. If that's what he means, then we need to understand what constitutes a 'date value', don't we?

                  Does he mean the actual 'date' data type used in SQL or RPG? If so, that format MUST have a day, it can't be "without days", can it? And, your C example doesn't provide a date data type, anyway, unless I'm missing something? Instead, it provides the date as a character buffer, containing the days and also the time. Surely that wasn't what he meant by a "date without days"?

                  But, of course, the value he provided is already the year and month portion of a *CYMD date. Maybe he wants a 4-digit year instead of the 3-digit one he has? If so, wouldn't it be much simpler to do myVar = myVar + 190000, rather than call an API and then have to substring off the part he doesn't want? But, of course, we don't know if that's what he wants.

                  Do you see why I'm asking him for more information, now?

                  Comment


                  • #10
                    Re: Convert number CYYMM into a date

                    Scott,

                    Like you I have no more information than he posted. Yes my sample entered a character string, that same character string can be built by
                    sprintf(value,"%D(5,0)01000000000",test);
                    where test is a decimal(5,0) value which needs to be converted.
                    It is just a suggestion based on what I read from his post, if its useful he can use it, if its not he can just ignore it.. The output can be varied to give him the date format he wants, if it really just needs to be a character string with CYYMM then I would just use
                    sprintf(value,"%D(5,0)",test);
                    and not call the API at all.

                    Chris...
                    Last edited by chris_hird; January 27, 2015, 05:41 PM.

                    Comment

                    Working...
                    X