- Why do I get an error when I move zero to a date?
- How do I validate that the date in the Date data type is a valid date?
- If I have an ISO date and an USA date do I have to convert them to the same date format before I can compare them?
- Why don't you initialize you dates using UDATE or *DATE?
- Why do I get an error message when I move a date from one Date data type to another
Why do I get an error when I move zero to a date?
Date data types can only contain valid dates. Zero is not a valid date, therefore, if you try and move it into a Date data type you will get a RNQ0112 error, "Date, Time or Timestamp value is not valid".
If you want to move the lowest value to a date field try:
ISO_date = *loval ;
In this example: ISO_date = 0001-01-01
The same will apply if you want to move all 9s to a date, as that is not a valid date. Replace that with moving *HIVAL into your Date data type.
You might this section from Tuesday useful, see here.
How do I validate that the date in the Date data type is a valid date?
You do not have to. If a value is in a Date data type then it has to be a valid date.
After publishing this post I received this comment from Jon Paris:
If the date field is in a DS then it is perfectly possible for it to contain an invalid date. Trust me - I've seen it happen.
He is correct. If the Date data type is a subfield in a data structure it can contain an invalid date. I have seen the same with numeric subfields.
I should have said: With standalone Date data type variables you do not have to.
If I have an ISO date and an USA date do I have to convert them to the same date format before I can compare them?
You can compare any formatted Date data type with another in a different format. For example:
ISO_date = d'2015-10-22' ; MDY_date = d'2015-10-22' ; if (ISO_date = MDY_date) ; dsply (%char(ISO_date:*iso-) + ' = ' + %char(MDY_date:*usa/)) ; endif ;
DSPLY 2015-10-22 = 10/22/15
Why don't you initialize you dates using UDATE or *DATE?
UDATE and *DATE contain the job date. As the environment I work in is 24 hours a day/6 days a week I am wary of using any "date" that is not the current/system date. If a job starts before midnight and continues into the next day UDATE and *DATE will contain the previous day's date.
I advise you all to use the %DATE built in function to get today's date. If you use it with no parameter it returns the current date:
ISO_date = %date() ;
If you really want to have the job date then I do this in the definition specifications:
dcl-s ISO_date1 date(*iso) inz(*job) ; DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++ D ISO_date2 S D datfmt(*iso) inz(*job)
You can learn more about initializing with special values here.
Why do I get an error message when I move a date from one Date data type to another
When you get an error RNQ0114, "The year portion of a Date or Timestamp value is not in the correct range", moving one Date data type to another it is all about how they are formatted.
Each date format can only contain a range of years. If move a date with a year that is outside of that range you get an error. These are the accepted range of years by Date data type:
|*DMY *MDY *YMD||1940 – 2039|
|*CYMD *CMDY *CDMY||1900 – 2899|
|*ISO *USA *EUR *JIS||0001 – 99999|
I have seen this happen the most when an *ISO Date data type containing its default value, 0001-01-01, is moved to a *MDY. Year 0001 just does not go into the range 1940 - 2039.
Learn more about date formats here.
Others in this series include: