Compare DB2 Date with RPG date - db2

I have this doubt on how to compare DB2 date, time fields with RPG date and time fields
If (Zpschdt < CurDat or (ZPschdt = Curdat and
(%Time() - ZPschtm) > 30));
For example in the above piece of code, Curdat is a character field which contains date value populated using below line
CurDat = %CHAR(%DATE():*MDY);
%Time() that is the current time needs to be subtracted from ZPschtm which is a DB2 time field and needs to be checked if the difference is greater that 30 minutes. How can this be achieved?

You can't compare a character variable to a date variable.
Generally, you have to either convert the character to date, or the date to character.
Since you character variable is formatted *MDY, it'd be easier to convert it to date; otherwise you'd need to change the format to YYYYMMDD in order to compare it as a numeric or character.
If Zpschdt < %Date(CurDat:*MDY)...

If you want to find schedule dates and times that are more than 30 minutes old, and the timeframe needs to cross days, you are going to have to use timestamps rather than dates and times. To deal with your situation, define a timestamp field, assign the schedule date and time, add 30 minutes to the timestamp, and compare that to the current timestamp. Like this:
dcl-s ts Timestamp;
ts = zschdt + zschtm + %minutes(30);
if ts < %timestamp();
// do something here
endif;

Think I have found a good enough solution for this as below:
If ( %Diff(%Date():Zpschdt:*Days) > 1 or
(%Diff(%Date():Zpschdt:*Days) = 0 and
%Diff(%Time():ZPschtm:*Minutes) > 30));
The first condition of Or - %Diff(%Date():Zpschdt:*Days) > 1 checks if Current date is ahead of the change date at least by one day.
The Second condition :
(%Diff(%Date():Zpschdt:*Days) = 0 and
%Diff(%Time():ZPschtm:*Minutes) > 30))
checks if the Change date is same as current day and if Current time is ahead of change time by at least 30 minutes..
Does this look good enough? Seems to do the job..

Related

Modify timestamp in datatime field in DB2

Is there a way to modify the timestamp alone for an existing datetime attribute, for example changedate currently holds the value 2002-10-18 00:00:00.0 and would like to change it to 2002-10-18 08:00:00.0 The date would remain the same and time would only change.
If you know the time portion of the timestamp is 00:00:00... and you want it to be a a set difference away, then you can simply add the appropriate number of hours (and/or minutes and or seconds)
-- just updating the hour
update myTable
set myTS = myTs + 8 hours;
-- updating hour, minutes, seconds
update mytable
set myTS = myTs + 8 hours + 35 minutes + 22 seconds;
If it's possible that the time portion has something besides 00:00:00, then you can make use the scalar timestamp() function which returns a timestamp from individual date and time parameters.
update mytable
set myTS = timestamp(date(myTS), time('08:00:00'));
You can use a regular UPDATE statement, then set the column value to the new timestamp value. To do so, you could utilize the date / time / timestamp-related scalar functions.
Not tested, but something like:
new value = DATE(timestamp_column)+ 8 HOURS
It would extract the date portion and add the 8 hours. Else, use TIMESTAMP_FORMAT to compose the new value by extracting what you need and pass it in.

How do I filter my data for the same time last year - Power Query

I'm making an excel file that will be used for a report on sickness absence.
We are looking at the last 100 days (which I've been able to do with a simple filter in my Applied Steps) and we want to compare to the same time last year plus 100 days before that date AND look at 50 days after that date.
How can I filter for this in Power Query? Or do I have to write a custom formula? (In which case, does anyone know what and where to write it)
Idea is to be able to run this report every day/week and give updates on how we're doing in the winter in comparison to last year.
Any help on this will be greatly appreciated, thank you in advance!
Use the drop down filter atop the date column ... Date filters ... between ... and enter some random set of dates like 1/30/2019 through 1/30/2020
That generates this code
= Table.SelectRows(#"Changed Type", each [date] >= #date(2019, 1, 30) and [date] <= #date(2020, 1, 30))
edit it for your desired date range which could be like this
= Table.SelectRows(#"Changed Type", each [date] >= Date.AddDays(targetdate, -465) and [date] <= Date.AddDays(targetdate, -315))
I assume -465 which is last year (365 days prior) plus 100 days back, and -315 which is -365+50
As for what to use instead of targetdate ... you didn't tell us .. perhaps hardcode a date #date(2019,10,31), use whatever you had in your original filter, or maybe the maximum of the current date column
List.Max(#"priorstepname"[date])

SAS Date and substracting 1 Month

I have a date that is stored as a number that is 201401. I would like to subtract 1 month, so that is 201312 and not 201400.
Also, if there is a more efficient way, please suggest as well. I also have the date stored as 01Jan2014, and would be fine converting the SAS date at that point, so that I can create two new columns (with 1 month subtracted) so that they have the value 01Dec2013 and 201312. Also, a function for incrementing the month forward or backward would be much appreciated.
Thanks in advance.
If you store the date as a SAS date, you can use the function intnx to increment it by whatever period you like.
newdate = intnx('Month',olddate,1,'s');
If you store it as an integer like your original, you're on your own to figure that out. My answer: don't.
The prior answer works.
I just wanted to add, storing as Date in SAS is not the same as storing as integer. It may display 01JAN2014 but it represents a number, so you can still perform computations.
If you store the date as a SAS date, you can use the function intnx to increment it by whatever period you like.
newdate = intnx('Month',olddate,1,'s');

SAS - Create a month and year date from one integer

I have a data set in which month and year are in one variable and come in the form 200801 which equates to 2008, January. How can I create a SAS date from this integer?
I would like something in the form of Jan 2008 - anything so that SAS recognizes it as a date, as I then need to subtract this value from service date to find out how much time has elapsed since enrollment into the dataset until date of service.
Please also keep in mind that this is a variable, and I have thousands of observations. So I also need the data step/ function to do this for the entire variable.
Any help is appreciated!
You need to put it to a character variable, then input back to numeric. You can do that pretty easily.
date_var = input(put(date_var_orig,6.)||'01',yymmdd8.);
You can also do it this way:
date_var = mdy(mod(date_var_orig,100),1,floor(date_var_orig/100));
Both assume you want the day to equal 1; make a choice there if you want something else (like end of month or middle of month).

CMIS Query : how to get result of one date only

I want to get data from one date only, example: 2014-06-16
in CMIS reference I know that we can use = (equal) operator that I think the time must be precised.
The alternative that i thought is to do like below :
First:
SELECT * FROM cmis:document WHERE cmis:creationDate >= TIMESTAMP '2014-06-16T00:00:00.000Z' AND cmis:creationDate< TIMESTAMP '2014-06-17T00:00:00.000Z'
Second:
SELECT P.tsi:DATENUM as date_traitement, L.tsi:type as type, P.tsi:statut as statut
FROM tsi:lot AS L JOIN tsi:pli AS P ON L.cmis:name = P.tsi:lot
WHERE
(P.tsi:DATENUM >= TIMESTAMP '2014-06-16T00:00:00.000Z' AND P.tsi:DATENUM < TIMESTAMP '2014-06-17T00:00:00.000Z')
The first one is running perfectly, I've got data from the 16 june BUT in the seconde I don't know WHY but I still got data from 2014-06-17
Note: tsi:DATENUM type is datetime
So could you say what's wrong OR how to get data from ONE date only?
The second one should work. The timestamps you are using are in GMT. If your timestamps are stored with a time zone offset it could be the reason why you are seeing times from 6/17 when you expect to only see times from 6/16.