How to add working hours depending on weekday in sql - sql

I want to create a basic case logging system and when somebody opens a new issue, the issue is assigned a Sr_number with a given number of hours. For example Sr_number 1 is 4 hours, 2 is 6 hours, 3 is 8 hours and 4 is 24 hours.
Now adding hours onto a time stamp is easy but the catch is I need to take into account working hours which are 09:00 to 17:00 Monday to Friday.So if a case is given a 12 hour Sr_number and the deadline for this falls at 16:00 on a week day then the deadline is extended to the next working day. Basically the deadline is 12 working hours.And calculation should be 1 hour worked for the issue logged on same day and remaining 11 hours to next working day.
If in case it is sun, it should consider directly go to monday.
Example:
Case created on: 10/06/2015 12:04:39 PM- with Sr_number 1 (12 Hours) Deadline is now: 10/07/2015 12.05 PM
Make sense?
Another catch is I need to take into account hours On Hold and these two have to be only within working hours.
For some case,saturdays is working ,for some its holiday.
How should i proceed.
I tried performing datepart,dateadd and datediff functions.But i could find only weekday.
I am new at sql.Please guide for the same

Related

Date_diff with specific condition time start and time end

is it possible to have date_diff with specific start and end time?
let say my store are open from 8AM - 10PM, which is 14 Hours.
and I have a lot of stuff to sell during that time. One of the SKU is out of stock from 2022-11-01 06.00 PM until tomorrow 2022-11-02 11.00 AM.
Instead of calculate 24 hours, I just want to calculate only from opening store until it closed or until its restock. Meaning from 6PM to 11AM is 8 Hours
my query
select date_diff('2022-11-02 11.00 AM', '2022-11-02 06.00 PM', hour) from table
with the result 17 hours instead of 8 hours
There isn't a way to configure DATE_DIFF to do this for you, but it's possible to do what you want, with some effort.
You should convert your dates to timestamps (TIMESTAMP(yourdate) or CAST(yourdate AS TIMESTAMP)) and use TIMESTAMP_DIFF instead.
This will allow you to work with smaller intervals than days.
For your calculation, you ultimately need to find the total time difference between the two timestamps and then subtract the out-of-hours timeframe.
However, calculating the latter is not as simple as taking the difference in days and multiplying by 8 hours (10pm-6am), because your out-of-hours calculation has to account for weekends and possibly holidays etc. Hence it can get quite complex, which is where the solution in my first link might come in.

How to Calculate Days,Weeks,Month,Year difference from Today's Date in SQL?

I need to check one process occurred how many times in the selected time period. My time period saved as in Days,Months,Weeks or Year format. Example: 3 Years or 4 Weeks or 2 months or 5 days.. something like this.
How to calculate 4 weeks or 3 years or 2 months or 5 days from today's date?
Which means : Today is 23 May 2022
>> 4 Weeks past of today is 22 April 2022
>> 3 Years past of today is 23 May 2019
>> 2 Months past of today is 23 March 2022
>> 5 Days past of today is 18 May 2019
How to calculate this in procedure to check my condition?
I got no idea in this ,as I am new to stored procedure.
Referred many similar questions, found out need to use DATEDIFF , but dont know how to use for my requirement.
Kindly help
Maybe this query can give you clue how to calculate date
select
'today is '+convert(varchar,getdate(),106),
'4 weeks past of today is '+convert(varchar,DATEADD(WEEK,-4,getdate()),106),
'3 years past of today is '+convert(varchar,DATEADD(YEAR,-3,getdate()),106),
'2 months past of today is '+convert(varchar,DATEADD(MONTH,-2,getdate()),106),
'5 days past of today is '+convert(varchar,DATEADD(DAY,-5,getdate()),106)
hope this can help you

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.

How to get results from an EXACT date in MySQL? (Not an interval)

So in my query, I simply need to narrow down the results by whoever's status was last updated 3 weeks ago.
The line:
AND DATE_ADD(user_status_updated, INTERVAL - 21 DAY)
returns the results as an interval from now and 21 days from now. I am only interested in the exact date 21 days ago, but cannot find the right function to use.
I am writing automatic emails and part of this project is to check daily for users that have been inactive for 3 weeks, so it will always be a matter of NOW() minus 21 days. But I don't want the interval, I want the exact 21 days ago since last updated results.
Ok I found my solution.
WHERE user_status_updated= DATE_SUB(DATE(NOW()), INTERVAL 21 DAY)
The user_status_updated is a DATETIME field

Subtracting two dates (including hours and minutes) considering only working time vba or Excel

I need to subtract two dates (including hours and minutes), but I only need to consider working hours. That is, I need to omit lunch time (from 13 to 14 hrs), weekends and hours after 18 hrs and before 9 hrs of the following day, in a working day (from Mo to Fr). Any thoughts?
I don't mind if it's an Excel formula or a vba code.
I have this formula, but it doesn't omit lunch time:
9*(NETWORKDAYS(initial_time;ending_time)-1)-24*((MOD(initial_time;1)-MOD(ending_‌​time;1)))
Here's a possible solution. It assumes an 8 hour work days for all but the start and end date. Also that start date/time is 9:00 or after and end date/time is 18:00 or earlier and that both are on a weekday.
=(NETWORKDAYS(A2,B2)-2)*8+IF(MOD(A2,1)>0.58333,(TIME(18,0,0)-MOD(A2,1))*24,(TIME(18,0,0)-MOD(A2,1))*24-1)+IF(MOD(B2,1)>0.58333,(MOD(B2,1)-TIME(9,0,0))*24-1,(MOD(B2,1)-TIME(9,0,0))*24)
.58333 equates to 14:00. The formula:
multiplies networkdays * 8
+ hours from start date/time until 18:00 subtracting 1 hour if start time is before 14:00
+ hours from 9:00 until end date/time subtracting 1 hour if end time is after 14:00
Of course this doesn't take any holidays into account.