Is there a query that can add only business days , I used Date Add function but its still counting weekends? - sql

I'm setting up web-page to display Date for scheduling. Currently I'm using Date-add to add 5 business days, using Week , but it still takes into consideration of the weekend.
I've tried the following code below and it includes weekend
select convert(varchar,dateadd(dw,5,getdate()),101)
I'm expecting it to show 5 days from today not counting the weekend.

Related

How to identify the next working day using SQL code?

If I have sunday as non working day and Monday as Public holiday how to exclude these days while calculating the time difference between the record created date and the time when the record was called for the first time. Creation_time, Attempt_time are attributes available.
The data for holidays list is loaded in a separate table.

How does one get number of full days and half days from a start and end date

Background
I've been working on some reporting views that get a multi-day work shift and are supposed to do some calculations based on data, but I'm a bit stuck here.
A typical shift is either 3 calendar days usually 1 half-day and two full days, or a whole week consisting of 2 half-days (end and start) and 5 full days.
Specifications
I have the following specifications for what is a full day and half-day. These rules are based on regulation and can't be changed.
2 half-days != 1 full-day, the 2 halves is more "valuable"
Given a started_at iso datetime and end_at iso datetime
I want to get two numbers, full_days, and half_days
A half day is
A day at the start of the range starting at or after 12.00
A day at the end of the range which ends before 19.00
A full day is
A day within the range (traditional 24hours)
A day at the start of the range starting before 12.00
A day at the end of the range which ends at or after 19.00
I'm thinking either a row per full-day and half-day or an aggregated row with half_days and full_days as two separate columns would be ideal in the view to connect it with my other views.
Simplified model
I simplified the data model to leave out unnecessary columns.
create table if not exists [trip]
(
trip_id integer
constraint trip_pk
primary key,
started_at text default (datetime('now')),
end_at text default (datetime('now'))
);
And I'm a bit stuck with how I should design this query. A simple time delta doesn't work.
SQLFiddle with sample data and answers: http://sqlfiddle.com/#!5/de7551/2
You can solve this with a CTE which calculates the day span (number of days the shift spans). Since half days are always 1, 2 or 0 (only occur on end and start) we don't actually need to consider each day by itself.
You can use julianday to get the day as a number, however julian days start at noon so you'll need to subtract 0.5 to get the "actual" day for your calculation. Floor the ending day to avoid a to long span if the end time is later then the start time on each respective day, and round up the result to include partial days as a spanned day.
At this point we can calculate number of half days by checking the end and start. To get the number of full days we simply subtract the half days from the result.
with trip_spans as (
select
ceil(julianday(end_at)-0.5 - floor(julianday(started_at)-0.5)) day_span
, t.*
, (
iif(time(started_at) > time('12:00'), 1, 0)
+
iif(time(end_at) <= time('19:00'), 1, 0)
) half_days
from trip t
)
select
trip_spans.*
, day_span-half_days full_days
from trip_spans

How to adding month to date in and return in Date format in Robot Framework

I try to calculate month.
My Example code: 27/01/2020 + 20 months
Test Date
${PAYMENTDATE} Set Variable 27/01/2020
${PAYMENTDATE} Convert Date ${PAYMENTDATE} date_format=%d/%m/%Y result_format=%Y-%m-%d
${DATE} Add Time To Date ${PAYMENTDATE} 20 months result_format=%d/%m/%Y
log to console ${DATE}
But it not work, Could anyone help please?
In your code you are providing time value in months which is not valid. Unfortunately adding months to the date is not possible using the Robotframework DateTime library. From the DateTime documentation:
time: Time that is added in one of the supported time formats.
You need to provide the time value in one of the possible way e.g. You can provide days.
20 months approximates to 600 days and below code works without problem.
${DATE} Add Time To Date ${PAYMENTDATE} 600 days result_format=%d/%m/%Y
In case you are looking for exact days to be added for 20 months than you need to calculate exact number of days starting from the date you want to add 20 months to and provide it in above code instead of 600 days. You can easily find answers on how to calculate exact days using python like this here.

Slow Query when using multiple between

I'm including a new option on an already working query/report on business objects.
Basically it already gets the records that were created from the first of last month and today dates.
I want to add the records from the previous year's same dates (last month and previous year and today previous year).
When I introduced the OR operator to include a new between date the report that usually takes 2 minutes, won't run in less than 30 minutes.
This Works great:
Select
value1
creation
From
Table
Where
creation BETWEEN trunc((trunc(sysdate,'mm')-1),'mm') AND trunc(sysdate)
This takes more than 30 min:
Select
value1
creation
From
Table
Where
(creation BETWEEN add_months(trunc((trunc(sysdate,'mm')-1),'mm'),-12) AND add_months(trunc(sysdate),-12)
OR creation BETWEEN trunc((trunc(sysdate,'mm')-1),'mm') AND trunc(sysdate))

WHERE statement to choose record previous day but choose Friday record when current day is Monday Microsoft SQL

I need a WHERE statement where the date of the record is the previous day. I have the below code which will do this
WHERE DOC_DATE = dateadd(day,datediff(day,1,GETDATE()),0)
However I need this statement to get Friday's record when the current day is Monday. I have the below code but it will not work for me. No errors come back on SQL although no records results come back either. I have the below code for this
WHERE DOC_DATE = DATEADD(day, CASE WHEN datepart(dw,(GETDATE())) IN (2) then -3 ELSE -1 END ,0)
Important to add that this needs to be in a WHERE clause. This is for a Docuware administrative view I am creating. I have no control on how to write the SELECT statement, I only have access to edit the WHERE clause:
Here's a slightly "magical" way to compute the value that doesn't depend on any particular server settings such as datefirst. It's probably not immediately obvious how it works:
WHERE DOC_DATE = dateadd(day,datediff(day,'20150316',getdate()),
CASE WHEN DATEPART(weekday,getdate()) = DATEPART(weekday,'20150330')
THEN '20150313'
ELSE '20150315' END)
In the first line, we're computing the number of days which have elapsed since some arbitrary date. I picked a day in March 2015 to use as my base date.
The second line asks what today's day of the week is and if it's the same as some arbitrary "Known good" Monday. Just taking one value and comparing it to 2 depends on what your DATEFIRST setting is so I prefer not to write that.
In the third line, we decide what to do if it's a monday - I give a date that is 3 days before my arbitrary date above. If it wasn't a monday, we pick the day before.
Adding it all together, when we add the days difference from the arbitrary date back to one of these two dates from lines 3 and 4, it has the effect of shifting the date backwards 1 or 3 days.
It's can be an odd structure to see if you're not familiar with it - but combining dateadd/datediff and exploiting relationships between an arbitrary date and other dates computed from it can be useful for performing all kinds of calculations. A similar structure can be used for computing e.g. the last day of the month 15 months ago using just dateadd/datediff, an arbitrary date and another date with the right offset from the first:
SELECT DATEADD(month,DATEDIFF(month,'20010101',GETDATE()),'19991031')
As I said in a comment though, usually doing this sort of thing is only a short step away from needing to properly model your organisation's business days, at which point you'd typically want to introduce a calendar table. At one row per day, 20 years worth of pre-calculated calendar (adjusted as necessary as the business changes) is still less than 10000 rows.
You can try this.
WHERE DOC_DATE = DATEADD(DAY, CASE WHEN datepart(dw, GETDATE()) = 2 THEN -3 ELSE -1 END, CAST(GETDATE() AS DATE))