Dynamic Timestamp DB2 SQL - sql

Using DB2 SQL
I would like to query for records since 2:00 yesterday. I want a dynamic expression that frees me from having to manually enter the current date prior to running the query. The created_datetime attribute is of timestamp dataype.
For example:
select record_key, other_stuff
from table
where created_datetime > "2 o'clock PM yesterday"
Is this kind of dynamic timestamp comparison even possible? Eventually, I'd like to be able to do a window of time, which gets complicated!
select count(1)
from table
where created_datetime between "2 o'clock PM yesterday" and "2 o'clock PM today"
I am familiar with current date, but I am trying to conceptualize how I would leverage that. The following gets me close, but it includes everything 24 hours prior to whenever the query is run.
select count(1)
from table
where created_datetime between (currentdate - 1 day) and (currentdate # 2 o'clock PM)
I know this is some pretty basic territory, and I feel guilty posting this question, but my research has not turned up anything for me so far. I appreciate every ounce of time spent on my behalf.

Try these
select record_key, other_stuff
from table
where created_datetime > CURRENT DATE - 10 HOURS
select count(1)
from table
where created_datetime between (CURRENT DATE - 10 HOURS) and (CURRENT DATE + 14 HOURS)
select count(1)
from table
where created_datetime between (CURRENT DATE - 1 DAYS) and (CURRENT DATE + 14 HOURS)
From the IBM Dev Works Library : DB2 Basics: Fun with Dates and Times
There are heaps of samples there.
E.g.
You can also perform date and time calculations using, for lack of a
better term, English:
current date + 1 YEAR
current date + 3 YEARS + 2 MONTHS + 15 DAYS
current time + 5 HOURS - 3 MINUTES + 10 SECONDS

Try this with this Timestamp option in you where clause.
Below sample to query for between last 24 hours.
select
timestamp(CURRENT date - 1 days,(CURRENT time - 24 hours)),
timestamp(CURRENT date,CURRENT time )
FROM
sysibm.sysdummy1;

Related

How to get start & end of same week last year in DB2 using SQL?

I have a weekly report that uses these date parameters:
SELECT *
FROM TABLE
WHERE DATE_FIELD BETWEEN (CURRENT DATE - 8 DAYS) AND (CURRENT DATE - 2 DAYS)
This runs on Mondays to gather the previous week's data (Sun-Sat). What I want now is to run this for the same week of the previous year.
So for example, if the code above runs on Mon 29/06/20, it returns data from Sun 21/06/20 - Sat 27/06/20, i.e. week 26 of 2020. I want it to return data from Sun 23/06/19 - Sat 29/06/19, i.e. week 26 of 2019.
The report runs automatically so I can't just plug in the exact dates each time. I also can't just offset the date parameters to -357 and -367 days, as this gets thrown off by leap years.
I've searched for solutions but they all seem to rely on the DATEADD function, which my DB2 database doesn't recognise.
Does anyone know how I can get the result I'm looking for, please? Any advice would be appreciated! :)
The easiest way to do this is to build a calendar or dates table...(google sql calendar table)
Among the columns you'd have would be
date
year
month
quarter
dayofWeek
startOfWeek
endOfWeek
week_nbr
You can use the week() or week_iso() functions when loading the table, pay attention to the differences and pick the best fit for you.
Such a calendar table makes it easy to compare current period vs prior period.
If you assume that all years have 52 weeks, you can use date arithmetic:
SELECT *
FROM TABLE
WHERE DATE_FIELD BETWEEN (CURRENT DATE - (8 + 364) DAYS) AND (CURRENT DATE - (2 + 364) DAYS)
Because you want the week to start on a Monday, this doesn't have to take leap years into account. It is subtracting exactly 52 weeks -- and leap years do no affect weeks.
This gets more complicated if you have to deal with 52 or 53 week years.
A little bit complicated, but it should work. You may run it as is or test your own date.
SELECT
YEAR_1ST_WEEK_END + WEEKS_TO_ADD * 7 - 6 AS WEEK_START
, YEAR_1ST_WEEK_END + WEEKS_TO_ADD * 7 AS WEEK_END
FROM
(
SELECT
DATE((YEAR(D) - 1)||'-01-01')
+ (7 - DAYOFWEEK(DATE((YEAR(D) - 1)||'-01-01'))) AS YEAR_1ST_WEEK_END
, WEEK(D) - 2 AS WEEKS_TO_ADD
FROM (VALUES DATE('2020-06-29')) T(D)
);
The intermediate column YEAR_1ST_WEEK_END value is the 1-st Sat (end of week) of previous year for given date.
WEEKS_TO_ADD is a number of weeks to add to the YEAR_1ST_WEEK_END date.

SQL Date and TIme

I need to take the list of students with Application acceptance Date & time in University database. The report is sent on 6 PM evening to the management but the management can run it on 7 PM 8 PM or any time within the night. The application should be just 2 days ahead to the report running date.
ORACLE: I have created a query but that will give the application of entire 24 hours of a day. The problem with this query is when management runs a query on 6 PM and 8 PM , and any students are accepted in between this time. The result will be different.
select
to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from
tbl_application_accepted_date
where
to_date(application_accepted_date, 'DD-MON-rr:HH24:MI:SS') =
to_date(trunc(sysdate-2), 'DD-MON-rr:HH24:MI:SS')
;
If any application is accepted between 6 ahead I need the list in next days report. Means, I should have the list of accepted students from 6PM onwards of previous day to 5:59 application day.
I am getting application accepted after 6 PM, which I don't need in todays report, I should get this on next day report. The report is run on 4th OCT 2018
If it needs to cut off at 6pm, then just add 18/24 (18 hours) to the truncated date. You are also doing a lot of unnecessary casting. As long as application_accepted_date is a date field, then you can just compare it as a date.
select to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from tbl_application_accepted_date
where application_accepted_date >= trunc(sysdate-3)+18/24
and application_accepted_date < trunc(sysdate-2)+18/24
This will give you any applications starting at 6pm 3 days ago until just before 6pm 2 days ago.
EDIT: You could also do this with interval literals if you want. Same query as above, but more explicit in your intentions.
select to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from tbl_application_accepted_date
where application_accepted_date >= trunc(sysdate-3) + interval '18' hour
and application_accepted_date < trunc(sysdate-2) + interval '18' hour
Assuming that application_accepted_date is a DATE datatype in your data model, try this:
select
to_char(application_accepted_date, 'DD-MON-RR:HH24:MI:SS')
from
tbl_application_accepted_date
WHERE application_accepted_date BETWEEN to_date(To_char(Trunc(SYSDATE - 1),'YYYY-MM-DD')
||'18:00:00','YYYY-MM-DD HH24:MI:SS) and to_date(to_char(trunc(sysdate),'yyyy-mm-dd')||'17:59:59','yyyy-mm-dd hh24:MI:SS)
That should always return data from 6:00pm yesterday to 5:59:59pm today.

Find the minute difference between 2 date time

I need to get the difference between 2 date time in minutes(Time difference in minutes). And the last difference will be calculated based on 6 PM of every date.
Sample data: need result of last column
User_Name Date Time difference in minutes
User 1 1/1/06 12:00 PM 30
user 2 1/1/06 12:30 PM 315
user 3 1/1/06 5:45 PM 15
Here the date will be always in same date and the last user date difference calculated based on default value 6PM. Assuming the dates of any user will not cross 6PM time.
Please suggest how to write the query for the same.
You could use the lead window function.
I assume your table is called mytable and the date column is mydate (it is a bad idea to call a column Date as it is a reserved word).
select user_name,
round((lead(mydate, 1, trunc(mydate)+18/24)
over (partition by trunc(mydate) order by mydate)
- mydate) *24*60) as difference
from mytable
I found the solution.. if its not correct let me know
SELECT User_name,created_date,
trunc(to_number((cast(nvl(lead (created_date,1) OVER (ORDER BY created_date),TRUNC(SYSDATE) + (19/24)) as date) - cast(created_date as date)))*24*60) as difference
FROM users;

How do you filter to get data that is in between a certain time of day in IBM DB2

I am trying to add a filter condition in the DB2 database. I am new to it and come from an Oracle background. I am trying to get records with dates in between today at 4 AM and today at 5 PM only. I currently have the below query that returns zero results:
db2 => select datetimeColumn from datetimeExample WHERE datetimeColumn BETWEEN timestamp(current date) - 1 day + 4 hour AND timestamp(current date) - 1 day + 13 hour
DATETIMECOLUMN
--------------------------
0 record(s) selected.
And here is the data in the table that I believe should show but there is something wrong with condition statement, any help is appreciated
db2 => select * from datetimeExample
DATETIMECOLUMN
--------------------------
2016-06-16-09.38.53.759000
1988-12-25-17.12.30.000000
2016-12-25-17.10.30.000000
2016-06-16-04.10.30.000000
2016-06-16-05.10.30.000000
1988-12-25-15.12.30.000000
1988-12-25-14.12.30.000000
2016-06-16-12.10.30.000000
2016-06-16-07.10.30.000000
2016-06-16-08.10.30.000000
10 record(s) selected.
The query should work when you leave out the - 1 day. The reason is that timestamp(current date) returns the timestamp for today at zero hours. Then you add 4 hours and are at the required start time. Similar maths for the end time (and 5 pm should be + 17 hours).
select datetimeColumn from datetimeExample
WHERE datetimeColumn
BETWEEN timestamp(current date) + 4 hours AND timestamp(current date) + 17 hours

problem with SQL query in DB2

How can i write a query in DB2 for following thing:
The difference between current timestamp and a timestamp field in dB should be >=4 hours AND
<= 24 hours
You don't really give enough information to answer the question (i.e., do you want data only from the past, only in the future, etc), but let's assume you want the data where the timestamp column ("tscolumn") is more than 4 hours old and less than 24 hours old:
select *
from table t
where t.tscolumn between current timestamp - 4 hours
and current timestamp - 24 hours
If my assumption is wrong it's pretty easy to rewrite this to meet your requirements.
Try following
select * from tableName where
date <= DATEADD(Hour, -4, CURRENT_TIME) and
date date >= DATEADD(Hour, -24, CURRENT_TIME)
select *
from table t
where timestampdiff(8,char(current timestamp - time_from_table)) between 4 and 24
here timestamp(8, - refers hours, below are the values for different arguments.
Value Description
1 Fractions of a second
2 Seconds
4 Minutes
8 Hours
16 Days
32 Weeks
64 Months
128 Quarters
256 Years