extract data from 9pm the previous day to 9pm of current day in DB2 - db2

how can we extract data from 9pm the previous day up until 9pm of current day using a timestamp column in DB2, please help

This should work but I'm not at a machine to test it right now...
WHERE datefield BETWEEN TIMESTAMP(CURRENT DATE - 1 DAY, '21:00:00')
AND TIMESTAMP(CURRENT DATE, '21:00:00')

Related

Fetch Data between 2 specific dates automatically in DB2

Here I'm with another unusual requirement.
Ok, so I have BO webi report (db2 database), and the report is supposed to run on 5th of every month and then it should have only data between a certain billing cycle (26th to 25th of last month)
So basically if I run the report on 5th of March, it should have data of billing cycle** 26th Jan - 25th Feb**.
I know I can manually put the dates in the query every month and run the report, but this report is going to be a schedule. So has to run automatically.
Any ideas ? Any date functions that can particularly tells a query to run between those dates ?
have not tried anything yet, but trying to come up a syntax. no clue :(
If you have whatever date of the same month as 2022-03-05, then you may get your date intervals as follows.
WITH PAR (DT) AS (VALUES '2022-03-05'::DATE)
SELECT
DT - (DAY (DT) - 1) - 2 MONTH + 25 AS DATE_FROM
, DT - (DAY (DT) - 1) - 1 MONTH + 24 AS DATE_TO
FROM PAR;
DATE_FROM
DATE_TO
2022-01-26
2022-02-25

How to calculate the last week of every month?

I've been having a look around but I can't seem to find anything that answers this question. I want to calculate the date for the last week of every month. For example, the date for the last week of April 2021 is 26-04-2021. I want a date, not a week number.
I use Google Big Query, I do have a calendar table I could use to extract year and month.
Thanks,
Emily
Try date_trunc:
SELECT date_trunc(last_day(month1st, month), week(monday))
from unnest(GENERATE_DATE_ARRAY('2021-01-01', '2021-12-01', interval 1 month)) AS month1st;

convert interval to hours in monthly tables

I have 12 monthly tables, one for each month of the year 2019, records are order by an identifier (mmsi) and datetime (timestamp). I have calculated the interval (linetime2) between two consecutive rows. see below (August 2019):
Now I need to convert interval in hours in a new column. How can I do it? Can I run this:
SELECT EXTRACT(epoch FROM linetime2)/3600
or I have to take into account the number of days in a month and year?

Tableau, Week to Date, Month To Date, Year to Date parameter

I have a data set spanning 2 years and is updated daily, created a dashboard to give a view of incidents by date group. I have created a parameter using date trunc for Day/Week/Month/Quarter/Year. This is in Tableau.
I am trying to get the parameter to show a Week to date, Month to date and so on view. IE if on Weds 15th Dec I selected the weekly view, it would only show data for every week in the data set for Sat-Weds (My weeks go Sat-Fri) or the monthly view every month between 1st-15th
Ideally I am wanting this as a simple parameter for a drop down menu, I can do the week to date stuff as a rolling sum restarting every week in a separate table, but for ease I just need a date group function that will allow to do this.
Any suggestions would be appreciated.
Cheers
Pete
The solution is 5 parts:
Date Part Parameter
Max date [Max Date]
Dynamic date part of the Max date [Dynamic Date Part Max]
Dynamic date part of the historical dates [Dynamic Date Part]
Filter those date parts <= the Max date [Dynamic Date - Lass than Max]
Date Part Parameter
Max Date
This is the calculation you'd use with your dataset to find the max date.
{ MAX([Order Date]) }
In order to create a good example, I'm going to set my Max date to a specific date the falls in the middle of a week, in the middle of a month and middle of the year. I'm going use June 13th, 2018 as my Max Date.
So, if you want to follow along you can use the below date as your max date. You can also use this data set if you'd like.
DATE(#2018-06-13#)
Dynamic date part of the Max date
DATEPART([Select Date Part], [Max Date])
Dynamic date part of the Historical dates
DATEPART([Select Date Part], [Order Date])
Filter on Historical dates parts <= the Max Date
[Dynamic Date Part] <= [Dynamic Date Part Max]
Now that we have all the pieces we need let's check to make sure they are working as we would expect.
Looks like we're seeing all the days of the month that are <= the 13th.
When we change it to Day of the Week we see only the days of the week <= the 4th day of the week which is Wednesday when the week starts on Saturday.
Now let's calculate the running sum of sales along our dynamic date part to better help you with your example.
Drag the measure you want to calculate the running sum onto the label, then create a quick table calculation. We'll next need to edit this table calculation as so.
You'll then see your calculation working as you would expect.
Hope this was helpful. Happy vizzing!

How to get total experience in terms of date object

I have a condition here in which I will have total experience in terms of month and year. For example, two drop down will be there for asking total number of experience in month and year. So if I am working from 1 Jan 2012, then I will write total experience as 3 year and 11 months. Now I have to convert this 3 year and 11 months into date format so that I can save this into database
You could use java.util.Calendar:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.YEAR, year);
Date date = calendar.getTime();
As a word of caution, the day field would be set to today's date. Check the intended behaviour if the current day is outside of the bounds for the target month. For example, setting the month to February when calendar has a day field of 30. It might be wise to set the day to a known, valid value for every month (eg: 1) before setting the month and year.
Use DATE_SUB() function:
Try this:
SELECT DATE_SUB(DATE_SUB(CURRENT_DATE(), INTERVAL 3 YEAR), INTERVAL 11 MONTH);
You can use mysql's date_sub() function or <date> - interval <expression> unit syntax to subtract an interval from a date.
select date_sub(curdate(),interval '3-11' YEAR_MONTH) as start_date
UPDATE:
Following the conversation between the OP and #eggyal, the OP need to replace the period in the incoming data with - and construct an insert statement as follows:
insert into mytable (...,join_date,...) values (...,date_sub(curdate(),interval '3-11' YEAR_MONTH),...)