Thursday, December 8, 2016

Extracting Pieces of Binary Dates

Extracting Pieces of Binary Dates

We recently received the following inquiry about extracting out portions of year, month and day, from numeric dates: Support Team,
Here is what I need to do, I have an I2 field with a date stored in YYYYMMDD format and what I need to be able to do is redefine the field so I can get to each piece. example:
    booking-date[1],4 = Century
    booking-date[5],2 = Month
    booking-date[7],2 = Day
Our response:
There is a way to isolate year, month and day in one step. It just involves some simple math in order to do what you want. It is not entirely obvious how to do this though. Myfile is a file with a double integer date field called "a" in the format ccyymmdd, below is a method to extract each portion.
>in myfile
>def year,1,4,display
>def month,1,2,display
>def day,1,2,display
>ext year=a / 10000
>ext month=(a / 100) mod 100
>ext day=a mod 100
>list
>xeq
>IN MYFILE.NEIL.GREEN (0) >OUT $NULL (0)
YEAR            = 2005           MONTH           = 2
DAY             = 7
You can also use the $edit function to isolate portions of the date and make the date more readable.
>in myfile
>def dispdata,1,10,byte
>ext dispdata=$edit(a,"9999 99 99")
>list
>xeq
>IN MYFILE.NEIL.GREEN (0) >OUT $NULL (0)
DISPDATA        = 2005 02 07

No comments:

Post a Comment