I'm try to write Oracle SQL function. Currently i try to get number of working day with cut-off point at 4.30. In problem is, how can i get the number of working day in same day as 0.
Example :
Staff opened request at 9.30 AM
Staff close the request at 4.15 (which is before cut off time)
So, the number of working day for example above should be 0 because it before cut-off time.
You could set the current day at 16.30 as a reference date and then calculate the difference between that date and the request date:
SELECT TRUNC(TO_DATE(TO_CHAR(SYSDATE,'DD-MM-RRRR') || ' 16:30','DD-MM-RRRR HH24:MI') - TO_DATE('09-12-2015 09:30','DD-MM-RRRR HH24:MI')) days
FROM DUAL
Related
I am trying to round upwards to the nearest month. So far, I have:
SELECT ROUND(CURRENT_DATE, 'MM') FROM DUAL
Which rounds to the closest month, which is upwards in this case. At the time of posting, the output is 03/01/2019 in MM/DD/YYYY format.
But what if it's the first of a month for example?
SELECT ROUND(TO_DATE('01-03-19','DD-MM-YY'), 'MM') FROM DUAL
This produces the same output as above. But I am expecting 04/01/2019.
I could do something like:
SELECT TRUNC(ADD_MONTHS(TO_DATE('30-03-19','DD-MM-YY'),1), 'MM') - 1 FROM DUAL
Which produced the output 03/31/2019, which is as expected.
I take the 30th March, add one month onto it. Truncate that, to get the first day of that month, then just subtract one for the last day of the previous month.
Now this works, but it seems long and tedious. Surely there is a better way?
if you are trying to find last day of the current month
LAST_DAY(SYSDATE)
if you are trying to find first day of the next month
LAST_DAY(SYSDATE) + 1
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.
Using PostgresQL 9.6, for a certain period of time, I have three input values:
"endDate": any date, being the end date
"months": a number of months between 0 and ca. 30
"days": a number of days between 0 and 29
The task is: Find the start date of that period. Requirement is: The result of age("end","start") (*) must always be the same as the input interval (month + days). In other words: Make the period storable with just start and end without explicitly saving the given interval, but making sure that the interval remains exactly the same.
My first simple attempt was
SELECT "endDate" - ("months" || ' mons ' || "days" ' days')::interval
However, that doesn't work for input
"endDate": 2017-06-29
"months": 4
"days": 19
The start date calculated by this approach will be 2017-02-09. And age('2017-06-29','2017-02-10') will return 4 mons 20 days which is one day too much.
The probable reason is that the minus operator most likely will first subtract the month, then if it lands on an "impossible date" like 2017-02-29, go to the previous "possible" date (here: 2017-02-28) and then subtracts the days, landing on 2017-02-09 - which is wrong here.
So, I came up with this idea:
WITH prep AS
( SELECT ("months"||' mons')::interval AS moninterval,
("days" || ' days')::interval AS dayinterval )
SELECT
CASE WHEN date_part('day',"endDate") > "days"
THEN (("endDate" - dayinterval) - moninterval)::date
ELSE ("endDate" - ( moninterval + dayinterval) )::date
END AS simstart
FROM prep
Basically, the idea is: If the number of days in the end date is bigger than the number of days in the interval, then first subtract the days, then the months. Otherwise, do as before.
That works for many cases. However, I still found an edge case where it doesn't:
"endDate": 2018-03-02
"months": 0
"days": 28
Regardless of the method used: The start date will always be calculated as 2018-02-02. And if you do SELECT age_forward('2018-03-02','2018-02-02') you will always end up with 1 mon - which is technically correct. However, it's wrong here, since the original input was 28 days.
(*) To be more precise: age_forward. See my answer to my own question here https://stackoverflow.com/a/51173709/2710714 However, I think it doesn't matter with the edge case problems described above.
Alright so I am trying to retrieve data a field we will call DATE_OF_ENTRY and the field is like this.
Example DATE_OF_ENTRY Data
28-NOV-15
So I need to use this field in a script that will be running twice a month to pull certain records. Basically when it's the 16th day of the current month I want all the records from the 1st-15th to be pulled up. When I run this script on the 1st of the next month I want all the records from the 16th-End of last month.
What I am using now
WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') AND DATE_OF_ENTRY < trunc(sysdate)
The problem with this statement is that it works on the 1st for the 16th to End of the last month, but on the 16th it gets data from the prior month still.
Any help is appreciated!
Using TRUNC() function with MONTH parameter will get the first day of the month.
Using TRUNC() function with DATE_OF_ENTRY will remove the TIME part.
Use + operator to add days to a DATE
SELECT TRUNC(sysdate, 'MONTH') firstDay,
TRUNC(sysdate, 'MONTH') + 15 Day15,
*
FROM yourTable
WHERE TRUNC(DATE_OF_ENTRY) >= TRUNC(sysdate, 'MONTH')
AND TRUNC(DATE_OF_ENTRY) <= TRUNC(sysdate, 'MONTH') + 15
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;