I have a date range say 1-Jul-2016 to 10-Jul-2016. I want to extract number of weeks and remaining days exists in that range like
No. of Weeks = 1
No. of Remaining Days = 3
I know how to find days OR week difference using
DATEDIFF(DAY,#ArrivalDate,#DepartureDate)
DATEDIFF(WEEK,#ArrivalDate,#DepartureDate)
But how to find and get whether a date contains only full Weeks or Weeks and Days.
Any help will be highly appreciated.
Try following query
DECLARE #StartDate DATETIME = '2016.07.01'
DECLARE #EndDate DATETIME = '2016.07.10'
SELECT
DATEDIFF(DAY, #StartDate, #EndDate) / 7 AS 'No. of Weeks', -- 1
(DATEDIFF(DAY, #StartDate, #EndDate) % 7) + 1 AS 'No. of Remaining Days' -- 3
DECLARE #StartDate DATETIME = '2016.07.01'
DECLARE #EndDate DATETIME = '2016.07.10'
SELECT
-- DATEDIFF(DAY, #StartDate, #EndDate) / 7 AS 'No. of Weeks', -- 1
DATEDIFF(WEEK, #StartDate, #EndDate) AS 'No. of Weeks',
(DATEDIFF(DAY, #StartDate, #EndDate) % 7) + 1 AS 'No. of Remaining Days' -- 3
Related
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SET #StartDate = '2012/11/01'
SET #EndDate = '2012/11/05'
SELECT
(DATEDIFF(wk, #StartDate, #EndDate) * 2)
+(CASE WHEN DATENAME(dw, #StartDate) = 'Sunday' THEN 1 ELSE 0 END)
+(CASE WHEN DATENAME(dw, #EndDate) = 'Saturday' THEN 1 ELSE 0 END)
Help to add # of holiday days to this solution? If there are any Holiday days between your start and end dates. Assuming I do have a table called BICalendar with a column isHoliday and value = 1 if that date is a holiday.
To directly answer the question:
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SET #StartDate = '2012/11/01'
SET #EndDate = '2012/11/05'
SELECT (DATEDIFF(wk, #StartDate, #EndDate) * 2) -- To calculate the number of weekend dates between the two input dates
+ (CASE WHEN DATENAME(dw, #StartDate) = 'Sunday' THEN 1 ELSE 0 END) --If the Start date is Sunday, then add one to adjust for non-working days
+ (CASE WHEN DATENAME(dw, #EndDate) = 'Saturday' THEN 1 ELSE 0 END) --If the End date is Saturday, then add one to adjust for non-working days
- (SELECT COUNT(*) --Minus the number of holiday dates between the two dates
FROM BICalendar
WHERE [Date] BETWEEN #STartDate AND #EndDate
AND isHoliday = 1);
I suspect what you're trying to do is calculate the number of working days between the two dates? If so, the below solution should also work:
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SET #StartDate = '2021-05-01'
SET #EndDate = '2021-05-15'
SELECT DATEDIFF(dd, #StartDate, #EndDate) --To calculate the number of days between the two dates
- (DATEDIFF(wk, #StartDate, #EndDate) * 2) -- To remove the weekends between the two dates
+ (CASE WHEN DATENAME(dw, #StartDate) = 'Sunday' THEN 1 ELSE 0 END) --If the Start date is Sunday, then add one to adjust for non-working days
+ (CASE WHEN DATENAME(dw, #EndDate) = 'Saturday' THEN 1 ELSE 0 END) --If the End date is Saturday, then add one to adjust for non-working days
- (SELECT COUNT(*) --Minus the number of holiday dates between the two dates
FROM BICalendar
WHERE [Date] BETWEEN #STartDate AND #EndDate
AND isHoliday = 1);
I have two dates: CREATION_DATE and START_DATE. START_DATE will always be later than CREATION_DATE. I need to calculate the number of minutes between them, except for minutes which happen on a weekend.
Every solution I can find assumes one of those dates occurs on a weekend, but alas, if CREATION_DATE is on a Friday, and START_DATE is a Monday, all of Saturday and Sunday is counted.
I've even tried calculating minutes from CREATION_DATE to the next 12am occurs plus minutes from first 12am Monday to START_DATE, but that doesn't work either.
I have found a solution if I only wanted to count days. I need to know down to minutes.
Our DB is hosted an I am not able to create VB functions so my solution must be all SQL.
The basic idea is to generate a record for all minutes between the start and finish, including those on weekends. Then use the WHERE clause to filter out those you don't want. In many cases, this is done by joining to a Calendar table, so you can also look at holidays or other special events, but for this purpose we can just use the DATEPART() function.
One this is done, we use a GROUP BY to roll things back up to the original date values and the COUNT() function to know how much work we did.
This basic concept works whether you're counting days, minutes, months, whatever.
It's not clear in the question, but I'm gonna assume your start and end values are columns in a table, rather than variable names (no #).
WITH Numbers(Number) AS
(
SELECT ROW_NUMBER() OVER (ORDER BY s1.[object_id]) - 1
FROM sys.all_columns AS s1
CROSS JOIN sys.all_columns AS s2
)
SELECT t.CREATION_DATE, t.START_DATE, COUNT(*) AS Num_Minutes
FROM [MyTable] t
INNER JOIN Numbers n on n.Number <= DATEDIFF(minute, t.CREATION_DATE, t.START_DATE)
WHERE DATEPART(dw, DATEADD(minute, n.Number, t.CREATION_DATE)) NOT IN (7,1)
GROUP BY t.CREATION_DATE, t.START_DATE
But this has the potential to be very slow, depending on how far apart the dates are. You can improve this by using various other ways to generate the Numbers table to get a starting point that better approximates the needs of your actual data.
If you aren't worried about accounting for holidays, you can do this as a simple math problem without having to monkey around with a tally table or doing any counting.
The following works by dropping the time portion off the begin and end date parameters and calculates the number of working days from that and multiples that figure by 3660. From there, if the begin date is a week day the begin date mins are subtracted... if end date is a weekday, those mins are added.
DECLARE
#BegDate DATETIME = '2018-09-13 03:30:30',
#EndDate DATETIME = '2018-09-18 03:35:27';
SELECT
working_mins = bm.base_mins
- ((1 - (x.is_beg_sat + x.is_beg_sun)) * x.beg_mins) -- if the begin date is a week day, subtract the mins from midnight.
+ ((1 - (x.is_end_sat + x.is_end_sun)) * x.end_mins) -- if the end date is a week day add the mins from midnight.
--,*
FROM
( VALUES (
DATEADD(DAY, DATEDIFF(DAY, 0, #BegDate), 0),
DATEADD(DAY, DATEDIFF(DAY, 0, #EndDate), 0)
) ) d (beg_date, end_date)
CROSS APPLY ( VALUES (
DATEDIFF(MINUTE, d.beg_date, #BegDate),
DATEDIFF(MINUTE, d.end_date, #EndDate),
DATEDIFF(DAY, d.beg_date, d.end_date),
DATEDIFF(WEEK, d.beg_date, d.end_date) * 2,
1 - SIGN(DATEPART(WEEKDAY, d.beg_date) % 7),
1 - SIGN(DATEPART(WEEKDAY, d.end_date) % 7),
1 - SIGN((DATEPART(WEEKDAY, d.beg_date) + 7) % 8),
1 - SIGN((DATEPART(WEEKDAY, d.end_date) + 7) % 8)
) ) x (beg_mins, end_mins, total_days, weekend_days, is_beg_sat, is_end_sat, is_beg_sun, is_end_sun)
CROSS APPLY ( VALUES (1440 * (x.total_days - x.weekend_days + x.is_beg_sat - x.is_end_sat)) ) bm (base_mins);
You can take a look at this solution, and see if meets your needs. Basically, I did the following:
Take the number of whole days betwen StartDate and EndDate that aren't weekend days, and multiply by 2:
SELECT COUNT(*) * 24 * 60 FROM WholeDaysBetween WHERE wkday <= 5
Take the minutes from StartDate (hours*60 + minutes)
(24 * 60) - (DATEPART(HOUR, #StartDate) * 60) - (DATEPART(MINUTE, #StartDate))
Take the minutes from EndDate (hours*60 + minutes)
(DATEPART(HOUR, #EndDate) * 60) + (DATEPART(MINUTE, #EndDate))
To get the number of whole days between, I used a recursive CTE:
WITH
WholeDaysBetween(dt, wkday) AS
(
SELECT DATEADD(DAY, 1, #StartDate), DATEPART(WEEKDAY, DATEADD(DAY, 1, #StartDate))
UNION ALL
SELECT DATEADD(DAY, 1, dt), DATEPART(WEEKDAY, DATEADD(DAY, 1, dt))
FROM WholeDaysBetween
WHERE dt < DATEADD(DAY, -1, #EndDate)
)
Of course, for this to work, you have to adjust your datefirst settings.
The final query is as follows (I used the same sample data as in your comment):
set datefirst 1; -- day starts on Monday
declare #StartDate datetime = '2018-09-21 23:59:00';
declare #EndDate datetime = '2018-09-24 00:01:00';
WITH
WholeDaysBetween(dt, wkday) AS
(
SELECT DATEADD(DAY, 1, #StartDate), DATEPART(WEEKDAY, DATEADD(DAY, 1, #StartDate))
UNION ALL
SELECT DATEADD(DAY, 1, dt), DATEPART(WEEKDAY, DATEADD(DAY, 1, dt))
FROM WholeDaysBetween
WHERE dt < DATEADD(DAY, -1, #EndDate)
)
SELECT
-- whole weekdays between #StartDate and #EndDate,
-- multiplied by minutes per day
(
SELECT COUNT(*) * 24 * 60
FROM WholeDaysBetween
WHERE wkday <= 5
)
+
-- minutes from #StartDate date to end of #StartDate
-- as long as #StartDate isn't on weekend
(
SELECT
CASE
WHEN DATEPART(WEEKDAY, #StartDate) <= 5
THEN
(24 * 60) -
(DATEPART(HOUR, #StartDate) * 60) -
(DATEPART(MINUTE, #StartDate))
ELSE 0
END
)
+
-- minutes from start of #EndDate's date to #EndDate
-- as long as #EndDate isn't on weekend
(
SELECT
CASE
WHEN DATEPART(WEEKDAY, #EndDate) <= 5
THEN
(DATEPART(HOUR, #EndDate) * 60) +
(DATEPART(MINUTE, #EndDate))
ELSE 0
END
)
DATEDIFF(MONTH, '1/1/2014', '12/31/2014') + 1
This will give me 12.
What if I do this:
DATEDIFF(MONTH, '1/1/2014', '12/30/2014') + 1
It should give me 11 point something. How do I go about getting the exact number of months between these two dates? This needs to work for any combination of dates (any month of the year for any year).
You could do the calculation yourself in the following way:
DECLARE #startdate date = '1/1/2014'
DECLARE #enddate date = '12/30/2014'
DECLARE #startday int = DATEPART(DAY, #startdate)
DECLARE #endday int = DATEPART(DAY, #enddate)
DECLARE #startdateBase date = DATEADD(DAY, 1 - #startday, #startdate)
DECLARE #enddateBase date = DATEADD(DAY, 1 - #endday, #enddate)
DECLARE #deciMonthDiff float = CAST(DATEDIFF(MONTH, #startdate, #enddate) AS float) -
(#startday - 1.0) / DATEDIFF(DAY, #startdateBase, DATEADD(MONTH, 1, #startdateBase)) +
(#endday - 1.0) / DATEDIFF(DAY, #enddateBase, DATEADD(MONTH, 1, #enddateBase))
SELECT #deciMonthDiff
This calculates the #deciMonthDiff to be 11.935483870967.
Of course you can "inline" this as much as you want in order to avoid all the middle declarations.
The idea is to calculate the total month diff, then subtract the relative part of the first & last month depending on the actual day.
DATEDIFF with the MONTH option only returns an integer value. Using days or years would give you a rough "guesstimate" but still not exactly right (different number of days in a month/year so you can't just divide the days difference by 30).
If you want exact you would need to write your own function to walk through the months from start until end and account for how many days are in each month and get a percentage/factor of that month covered.
DECLARE #startdate date = '1/1/2014'
DECLARE #enddate date = '12/30/2014'
select case when DATEPART(DAY, #startdate) <= DATEPART(DAY, #enddate)
then datediff(month, #startdate, #enddate)
else datediff(month, #startdate, #enddate) -1
end
from Chris The Jedi of T-SQL
This question already has answers here:
Count work days between two dates
(24 answers)
Calculating days to excluding weekends (Monday to Friday) in SQL Server
(6 answers)
Closed 7 years ago.
I'm looking for a way to calculate the days between two dates, but on weekdays. Here is the formula, but it counts weekend days.
DATEDIFF(DAY,STARTDATE,ENDDATE)
SELECT DATEDIFF(DAY,'2015/06/01' , '2015/06/30')
Result of above query of datediff is 29 days which are weekend days. but i need week days that should be 21 by removing Saturday and Sunday(8 days).
Any suggestions?
Put it in the WHERE clause
SELECT DATEDIFF(DAY,'2015/06/01' , '2015/06/30')
FROM yourtable
WHERE DATENAME(dw, StartDate) != 'Saturday'
AND DATENAME(dw, StartDate) != 'Sunday'
Or all in a SELECT statement
SELECT (DATEDIFF(dd, StartDate, EndDate) + 1)-(DATEDIFF(wk, StartDate, EndDate) * 2)-(CASE WHEN DATENAME(dw, StartDate) = 'Sunday' THEN 1 ELSE 0 END)-(CASE WHEN DATENAME(dw, EndDate) = 'Saturday' THEN 1 ELSE 0 END)
This returns 22:
DECLARE #StartDate AS DATE = '20150601'
DECLARE #EndDate AS DATE = '20150630'
SELECT
(DATEDIFF(DAY, #StartDate, #EndDate))
-(DATEDIFF(WEEK, #StartDate, #EndDate) * 2)
-(CASE WHEN DATENAME(dw, #StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, #EndDate) = 'Saturday' THEN 1 ELSE 0 END)
Read this article by Jeff Moden for more information.
Explanation:
First, (DATEDIFF(DAY, #StartDate, #EndDate)) will return the difference in number of days. In this case, it'll be 29. Now, depending on your interpretation of whole days, you may want to add 1 day to its result.
Next,(DATEDIFF(WEEK, #StartDate, #EndDate) * 2):
To quote the article:
DATEDIFF for the WEEK datepart doesn't actually calculate weeks, it
calculates the number of times a date range contains dates that
represent pairs of Saturdays and Sundays. To think of it in more
simple terms, it only counts WHOLE WEEKENDS!
So, to exclude the weekends, you must subtract twice the result of this from the first DATEDIFF. Which now will be: 29 - (2 *4) = 21.
Finally, this:
-(CASE WHEN DATENAME(dw, #StartDate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, #EndDate) = 'Saturday' THEN 1 ELSE 0 END)
removes the partial weeks, which only happens when then #StartDate occurs on a Sunday and the #EndDate occurs on a Saturday.
You can try recursive cte:
WITH cte
AS ( SELECT CAST('2015/06/01' AS DATE) AS dt ,
DATEPART(WEEKDAY, '2015/06/01') AS wd
UNION ALL
SELECT DATEADD(d, 1, dt) AS dt ,
DATEPART(WEEKDAY, DATEADD(d, 1, dt))
FROM cte
WHERE dt < '2015/06/30'
)
SELECT COUNT(*)
FROM cte
WHERE wd NOT IN ( 7, 1 )
Result is 22.
You better add some useful calendar table that every database should have. Fill it with some big range and then use it to calculate business days.
I wrote a simple stored procedure - don't know if a SP is what you need but i think you can easily convert it to a function , TVF, whatever:
CREATE PROCEDURE [dbo].[BusinessdaysBetween](#DateFrom DATE, #DateTo DATE, #days INT OUTPUT)
AS
BEGIN
DECLARE #datefirst INT = ##DATEFIRST
SET DATEFIRST 1 --so week starts on monday
SET #days = 0
IF #DateFrom > #DateTo
RETURN NULL
IF #DateFrom = #DateTo
RETURN #days
WHILE #DateFrom <= #DateTo
BEGIN
IF DATEPART(WEEKDAY,#DateFrom) NOT IN (6,7) --Saturday or Sunday
BEGIN
SET #days = #days + 1
END
SET #DateFrom = DATEADD(DAY,1,#DateFrom)
END
SET DATEFIRST #datefirst --restore original setup
END
GO
in my example i call i it like this:
DECLARE #days INT = 0
DECLARE #datefrom DATETIME = GETDATE()
DECLARE #dateto DATETIME = DATEADD(DAY,25,GETDATE())
EXEC dbo.BusinessdaysBetween #datefrom, #dateto, #days OUTPUT
SELECT #days
I'm calculating income of hiring equipments for a report. In that, hiring cost in weekend days will be plus 10% more if compare with normal days. So how can I calculate there's how many weekend days between two dates. And in report query, I can't use DECLARE also. Can someone help me to do this. Thank you so much
This should work:
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SET #StartDate = '2012/11/01'
SET #EndDate = '2012/11/05'
SELECT
(DATEDIFF(wk, #StartDate, #EndDate) * 2)
+(CASE WHEN DATENAME(dw, #StartDate) = 'Sunday' THEN 1 ELSE 0 END)
+(CASE WHEN DATENAME(dw, #EndDate) = 'Saturday' THEN 1 ELSE 0 END)
http://sqlfiddle.com/#!3/d41d8/5707/0