Number of business days (excluding holidays) ?
Hi All,
What is the SQL query to find number of business days (excluding holidays) between 2 given dates ?
List of Holidays will be maintained as a separate table.
I need a SQL query rather than a stored procedure or function.
I am using oracle 9i.
Thanks in advance.
Does this thread help?
Need to find number of business days - query pls
Similar Messages
-
Recursive CTE to get first business day excluding public holidays
I wanted to write a recursive CTE to calculate the first business day excluding the public holidays for a given year. I have a table with the public holiday for that same year
I like the functions I built to get this done:
create function NthDayOfMonth (@year int, @month smallint, @weekday varchar(15), @nth smallint)
returns datetime
as
begin
declare @the_date datetime, @c_date datetime, @cth smallint
set @cth = 0
set @c_date = convert(varchar,@year)+'-'+convert(varchar,@month)+'-01'
while month(@c_date) = @month
begin
if datename(weekday,@c_date) = @weekday set @cth = @cth + 1
if @cth = @nth and datename(weekday,@c_date) = @weekday set @the_date = @c_date
set @c_date = dateadd(day,1,@c_date)
end
return @the_date
end
GO
create function Holidays(@year int)
returns @table table
date date,
type varchar(10),
name varchar(25)
as
begin
insert into @table
select convert(datetime,convert(varchar,@year)+'-01-01') as date,'Holiday' as type ,'New Years Day' as name UNION ALL
select dbo.NthDayOfMonth(@year,2, 'Monday',3),'Holiday','Family Day' UNION ALL
select dateadd(d,0-case when datepart(weekday,convert(varchar,@year)+'-05-25') in (1,2) then 5+datepart(weekday,convert(varchar,@year)+'-05-25') else datepart(weekday,convert(varchar,@year)+'-05-25')-1 end, convert(varchar,@year)+'-05-25') ,'Holiday','Victoria Day' UNION ALL
select convert(varchar,@year)+'-07-01' ,'Holiday','Canada Day' UNION ALL
select dbo.NthDayOfMonth(@year,8, 'Monday',1),'Holiday','Civic Holiday' UNION ALL
select dbo.NthDayOfMonth(@year,9, 'Monday',1),'Holiday','Labour Day' UNION ALL
select dbo.NthDayofMonth(@year,10,'Monday',2),'Holiday','Thanksgiving' UNION ALL
select convert(varchar,@year)+'-11-11' ,'Holiday','Rememberance Day'UNION ALL
select convert(varchar,@year)+'-12-25' ,'Holiday','Christmas Day' UNION ALL
select convert(varchar,@year)+'-12-26' ,'Holiday','Boxing Day' UNION ALL
SELECT CONVERT(DATE,CONVERT(VARCHAR,@year) + '-0'+CONVERT(VARCHAR, FLOOR((((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + 90) / 25))+'-'+CONVERT(VARCHAR,CONVERT(INT,(((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + (FLOOR((((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + 90) / 25)) + 19) % 32,0))),'Holiday','Easter Sunday' UNION ALL
SELECT DATEADD(DAY,-2,CONVERT(DATE,CONVERT(VARCHAR,@year) + '-0'+CONVERT(VARCHAR, FLOOR((((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + 90) / 25))+'-'+CONVERT(VARCHAR,CONVERT(INT,(((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + (FLOOR((((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + 90) / 25)) + 19) % 32,0)))),'Holiday','Good Friday' UNION ALL
SELECT DATEADD(DAY,1,CONVERT(DATE,CONVERT(VARCHAR,@year) + '-0'+CONVERT(VARCHAR, FLOOR((((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + 90) / 25))+'-'+CONVERT(VARCHAR,CONVERT(INT,(((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + (FLOOR((((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) - (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) + ((((2 * FLOOR((@year / 100.0)) % 4) + (2*((@year % 100) / 4))) - ((@year % 100) % 4) - ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30) + (FLOOR((@year % 19.0 + (11 * ((( (19 * (@year % 19.0)) + FLOOR(@year / 100.0)) - FLOOR((@year / 100.0) / 4) - FLOOR((8 * (@year / 100.0) + 13) / 25.0) + 15) % 30))) / 319)) +32) % 7) + 90) / 25)) + 19) % 32,0)))),'Holiday','Easter Monday'
update @table
set date =
case when MONTH(date) = 12 AND name != 'Boxing Day' and datepart(weekday,date) = 7 then dateadd(day,2,date)
when MONTH(date) = 12 AND name != 'Boxing Day' and datepart(weekday,date) = 1 then dateadd(day,1,date)
when MONTH(date) = 12 AND name = 'Boxing Day' and datepart(weekday,date) = 7 then dateadd(day,2,date)
when MONTH(date) = 12 AND name = 'Boxing Day' and datepart(weekday,date) = 1 then dateadd(day,2,date)
when MONTH(date) = 12 AND name = 'Boxing Day' and datepart(weekday,date) = 2 then dateadd(day,1,date)
else date
end
return
end
GO
create function Dates(@date datetime)
returns @table table
now datetime,
today datetime,
Month_start datetime,
Month_end datetime,
Prev_Month_Start datetime,
Prev_Month_End datetime,
Week_Start datetime,
Week_End datetime,
Prev_Week_Start datetime,
Prev_Week_End datetime,
Quarter_Start datetime,
Quarter_End datetime,
Prev_Quarter_Start datetime,
Prev_Quarter_End datetime,
Year_Start datetime,
Year_End datetime,
Prev_Year_Start datetime,
Prev_Year_End datetime,
Month_End_TS datetime,
Prev_Month_End_TS datetime,
Week_End_TS datetime,
Prev_Week_End_TS datetime,
Quarter_End_TS datetime,
Prev_Quarter_End_TS datetime,
Year_End_TS datetime,
Prev_Year_End_TS datetime,
Year smallint,
Month smallint,
Day smallint,
Month_Name varchar(15),
Day_Name varchar(15),
WD SMALLINT,
Week INT,
isHoliday bit
as
begin
if @date IS NULL set @date = getdate()
insert into @table
select
@date as now,
convert(datetime,convert(varchar,@date,101)) as today,
dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101))) as Month_Start,
dateadd(day,-1,dateadd(month,1,dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101))))) as Month_end,
dateadd(month,-1,dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101)))) as Prev_Month_start,
dateadd(day,-1-day(@date)+1,convert(datetime,convert(varchar,@date,101))) as Prev_Month_End,
dateadd(day,1-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Week_Start,
dateadd(day,7-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Week_End,
dateadd(day,-6-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Prev_Week_Start,
dateadd(day,0-datepart(dw,@date),convert(datetime,convert(varchar,@date,101))) as Prev_Week_End,
convert(datetime,convert(varchar,year(@date)) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,@date)-1)*3)+1),2)+'-01') as quarter_start,
dateadd(day,-1,dateadd(quarter,1,convert(datetime,convert(varchar,year(@date)) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,@date)-1)*3)+1),2)+'-01'))) as quarter_end,
convert(datetime,convert(varchar,year(dateadd(quarter,-1,@date))) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,dateadd(quarter,-1,@date))-1)*3)+1),2)+'-01') as prev_quarter_start,
dateadd(day,-1,dateadd(quarter,1,convert(datetime,convert(varchar,year(dateadd(quarter,-1,@date))) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,dateadd(quarter,-1,@date))-1)*3)+1),2)+'-01'))) as prev_quarter_end,
dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101))) as Year_Start,
dateadd(year,1,dateadd(day,0-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101)))) as Year_End,
dateadd(year,-1,dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101)))) as Prev_Year_Start,
dateadd(year,-1,dateadd(year,1,dateadd(day,0-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101))))) as Prev_Year_End,
dateadd(ms,-3,dateadd(day,0,dateadd(month,1,dateadd(day,0-day(@date)+1,convert(datetime,convert(varchar,@date,101)))))) as Month_End_Ts,
dateadd(ms,-3,dateadd(day,-1-day(@date)+2,convert(datetime,convert(varchar,@date,101)))) as Prev_Month_End_TS,
dateadd(ms,-3,dateadd(day,8-datepart(dw,@date),convert(datetime,convert(varchar,@date,101)))) as Week_End_TS,
dateadd(ms,-3,dateadd(day,1-datepart(dw,@date),convert(datetime,convert(varchar,@date,101)))) as Prev_Week_End_TS,
dateadd(ms,-3,dateadd(day,0,dateadd(quarter,1,convert(datetime,convert(varchar,year(@date)) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,@date)-1)*3)+1),2)+'-01')))) as quarter_end_TS,
dateadd(ms,-3,dateadd(day,0,dateadd(quarter,1,convert(datetime,convert(varchar,year(dateadd(quarter,-1,@date))) +'-'+ right('0'+convert(varchar,((datepart(QUARTER,dateadd(quarter,-1,@date))-1)*3)+1),2)+'-01')))) as prev_quarter_end_TS,
dateadd(ms,-3,dateadd(year,1,dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101))))) as Year_End_TS,
dateadd(ms,-3,dateadd(day,1-day(@date),dateadd(month,1-month(@date),convert(varchar,@date,101)))) as Prev_Year_End_TS,
Year(@date) as Year,
Month(@date) as Month,
Day(@Date) as Day,
datename(month,@Date) as Month_Name,
datename(WEEKDAY,@date) as Day_Name,
datepart(weekday,@date) as WD,
DATEPART(WEEK,@date) AS Week,
COALESCE((SELECT 1
FROM dbo.Holidays(YEAR(@date)) h
WHERE h.date = @date),0) AS isHoliday
return
end
go
The holidays function is set up for Saskatchewan right now, but its a small matter to adjust it to your region.
You can then do this:
DECLARE @calendar TABLE (date DATE)
WHILE (SELECT COUNT(*) FROM @calendar) < 365
BEGIN
INSERT INTO @calendar (date)
VALUES (DATEADD(day,1,(SELECT COALESCE(MAX(date),CURRENT_TIMESTAMP) FROM @calendar)))
END
SELECT MONTH(date) AS month, MIN(date) AS fbd
FROM @calendar c
CROSS APPLY dbo.Dates(c.date)
WHERE DATEPART(DAY,c.date) NOT IN (1,7) AND isHoliday = 0
GROUP BY MONTH(date) -
Add number of business days to date field
Hello, I noticed that there is not much discussion in the forum surrounding u201Cbusiness daysu201D so I hope this post/discussion/solution contributes to the content of this forum.
I am using the following 3 formulas to try to Add number of business days (based upon a table field) to a specified date:
1. Variable name = SetVar
//Set Variable for Add Business Days:
DateVar Array Holidays;
DateVar Target:= CDate({TMS_MOVEMENT_EVT.EVT_DATE}); // Put your field name in here
NumberVar Add:= {DTCI_DOD_TRANS.TRANS}; // put the number of days here to add (a positive number)
NumberVar Added := (0);
2. Variable name = AddBizDays
//Adding Business Days:
EvaluateAfter({@SetVar});
WHILE Added < Add
Do (target := target +1;
if dayofweek (target) in 2 to 6 and not (target in holidays)
then Added:=Added+1
else Added:=Added);
Target
3. Variable name = HOLIDAYS
//Holiday Array Formula for the report Header:
BeforeReadingRecords;
DateVar Array Holidays := [
Date (2003,12,25), // you can put in as many lines for holidays as you want.
Date (2003,12,31)
0
I am successfully getting my data needed to make the necessary calculations and variable assignmentsu2026 I believe that my ISSUE is that I am not sure where to place these formulas into my report so they assign the variables and execute properly when previewing my report. I am curious if that is my issue, and if so, can someone provide me direction on where to put each of these formulas in my report.
Currently, when I try to preview the report, I get the following Crystal Reports Error: -u201CA number, currency amount, Boolean, date, time, date-time, or string is expected here.u201D
Then Crystal automatically opens the AddBizDays formula and highlights the word added, see below RE: u201CWHILE Addedu201D
For reference, my report has 3 groups, and I am displaying all of my output information (and locating my formulas) in the group footer #3 report section. I have moved them around to other report sections/groups/placements, but to no success.
Thanks so much.
Best, MattI worked this out... FYI - for the benefit of all forum users:
ADDING BUSINESS DAYS TO A CERTAIN DATE (excluding weekends and holidays)
1. Variable name = AddBizDays
//Adding Business Days:
WhileReadingRecords;
DateVar Array Holidays;
DateVar Target:= CDate(); // Put your field name in here
NumberVar Add:= ; // put the number of days here to add (a positive number)
NumberVar Added := (0);
WHILE Added < Add
Do (target := target +1;
if dayofweek (target) in 2 to 6 and not (target in holidays)
then Added:=Added+1
else Added:=Added);
Target
2. Variable name = HOLIDAYS
//Holiday Array Formula for the report Header:
BeforeReadingRecords;
DateVar Array Holidays := [
Date (2003,12,25), // you can put in as many lines for holidays as you want.
Date (2003,12,31)
0
...too bad i don't get forum points for sharing this solution.
Special thanks to KenHamady.com -- for sharing this solution.
Sincerely,
Matt -
Calculate number of Business Days
I need to calculate number of Business Days in BODS. Lets say I have One source table and One Date Dimension table to identify Business Day and need to calculate the no of working days and load in to FACT table (Like in screenshot below)?
By adding Query transform, I can calculate no of Days, but how to calculate working days between ORDER_RECEIVED_DATE and ORDER_SHIPPED_DATE.
SQL: select count(*) from DIM_DATE where DATE between ORDER_RECEIVED_DATE and ORDER_SHIPPED_DATE and HOLIDAY_FLAG='NO'Prashanth Chinta,
I have simulated this Job in my repository and and below are the steps by step implementation.
Your Dataflow will look like below. It contains the source and Target table with expected results. Hope this is what you are looking for.
DIM_DATE table contains all the dates in a year and mark each date either Holiday as 'YES' or 'NO'
Qry_Total_Ship_Days Query Transform
Qry_Join Query Transform
Qry_Filter_Holidays Query Transform
Qry_Cnt_Holidays Query Transform
Qry_Cnt_Holidays Query Transform (This is to show the groupby columns)
Qry_Cal_Ship_Days Query Transform -
Fucntion to Calculate business days (exclude weekends & holidays) between two days
Hello,
I need to be able to calculate business days between two dates excluding weekends and holidays. I do have a date dimension and also flags which denote weekday, holiday. Would really appreciate help in building a udf for the business days calculation.
Thanks,Hi, Please take a look and tweak accordingly. Best of luck.
--Assuming data dimension table something like the following
--tblDateDim (DT date primary key,
flagHoliday bit,
flagWeekday bit
CREATE FUNCTION fnBusinessDays (
@StartDate DATE,
@EndDate DATE
RETURNS INT
AS
BEGIN
DECLARE @Days INT ;
SELECT @Days = count(*)
FROM tblDateDim --// date dimension
WHERE
( DT BETWEEN @StartDate AND @EndDate) --// dt is date column in the table.
AND --// also flags which denote weekday, holiday
flagHoliday = 0 --// flagHoliday= 1--Holiday;= 0 --workingday/businessday
AND
flagWeekday = 1 --// FlagWeekday = 1--businessday ; =0 --weekend (Saturday and Sunday)
RETURN (@Days)
END
GO -
Calculating Business Days in a Date Range
I have two questions:
1. Does anyone know a better formula for calculating business days in totals?
I am currently using the formula in https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/701a52c3-6b1e-2b10-21b3-a6e101be1a0f
I tailored to my needs. However, there are still a lot of manual maintenance every year.
2. I have many reports that need the formula. It is very consuming to update the formula in each report. Does anyone know a better way to do it?
I use Crystal XI. 11.0.0.895. We do have a Crystal Enterprise server hosted in another department.Not sure if this is any simpler but you could save this as a custom function, that way you will have to modify it once a year for the holidays.
numbervar days;
datevar date1 := minimum({?My Parameter});
datevar date2 := maximum({?My Parameter});
days := DateDiff ("d", date1, date2) -
DateDiff ("ww", date1, date2, crSaturday) -
DateDiff ("ww", date1, date2, crSunday); // this will give you the number of business days
// (excluding Saturdays and Sundays) for a given date range.
// then, for each holiday, you can enter lines like this
if date(2008,01,01) in {?My Parameter} then days := days - 1;
// The final tally of DAYS should give you the total business days in a date range.
totext(days,0);
where {?My Parameter} is the date range. -
Calculate "Business Days" and account for holidays
I have played with Date math based on what I have found for documentation, and adding an arbitrary number of days is pretty easy. But what about adding an arbitrary number of business days? I found a web site that talks about "Next business day",
which could be adapted I am sure, but it only accounts for weekends. I want to calculate the date an arbitrary number of business days from a provided date. So, +3 business days calculated from a Monday should produce the date of the following Thursday,
and calculated from a Friday should produce the date of the next Wednesday. And calculated from Friday Sept 4, 2015 (Friday before Labor Day) should produce Thursday Sept 10, 2015.
Given that Windows is very business focused, I wonder if there is some nice hidden .NET functionality to calculate business days based on holidays as well? I know, some offices might give extra days off, four day weekends, etc. But those would be edge case
enough to be safely ignorable for my purposes. Indeed, even holidays could probably be ignored, but if there is a quick approach I would rather use it. If I would need to code some sort of Exchange calendar scraper or some such, I'll live with just accounting
for weekends. ;)You can pull holiday info from outlook.hol file and do the date math based on it. It won't be 100% reliable though. Not all the holidays listed in calendar are non-business days (not in all countries at least). International support may also present additional
issues: in some countries it is a common practice to "move" weekend days to fill a single day gap between holidays and weekend (which screws up next business day calc anyway, regardless of holiday info :-). Not to mention all the possible industry-wide
or company specific policies regarding working days.
Gleb. -
Oracle 8i: Business Days Calculation in a Subquery
To Whom It May Concern:
I apologize if this has been posted already, but I didn't find any threads that address my issue:
I need to find the number of business days between 2 dates (excludes weekends) provided by dates from an outer query such as the following:
SELECT
C.CUSTOMER_ID
, C.ENTRY_DATE
, C.COMPLETION_DATE
, <SOME SUBQUERY HERE THAT CALCULATES THE NUMBER OF BUSINESS DAYS BETWEEN C.COMPLETION_DATE AND C.ENTRY_DATE> AS BUSINESS_DAYS
FROM CUST_TABLE C
Thanks for your time and advice.
Maxyou don't need a subquery. it's a very simple formula
Re: query regarding dates
(you may need to change the case to a decode for v8 - I don't remember when it was introduced) -
Number of working days in customer exit?
Hi, this is actually an extension to the following thread.
How to calculate number of working days
I have very similar requirement. Determine the number of working days (excluding weekends and holidays) from 2 given dates. But the <b>difference here</b> is that, one of the date in the calculation is the<b> current date.</b>
1: Creation Date - this is present as infoobject in the ODS.
2: Current Date - this is 'today''s date.
And, No. of Working days(Zworkdays) = Current Date - Creation Date.
So, as mentioned in the thread 305257 speaks about including the logic in the update rules, I can use system variable sy-datum to get the current date in update routine. But if the load is not scheduled for every day (say,once in 2 days), then when i report this infoobject Zworkdays on Day2, then it will hold Day1 calc rite?
PLease correct me if I am wrong.
Could you please help me with other approachs I can follow. One I can think is Customer Exit in Bex for the same.... but I am not sure how to do the same (i am new to Bex and Customer exit). Could any one of you please guide me how to achieve the same. I Have 0CalDate in the ODS also.
Any other ideas/inputs is greatly appreciated.
Thanks in advance
RamyaHi,
search for virtual key figures.
Rgds.
Juergen
P.S. Thanks for assigning points
Message was edited by:
Jürgen Hemelt -
I would like to add x number of business days to a date extracted from Discoverer Plus. How might one do so?
Hi,
I think passing v_nextBusinessDate for checking holiday will solve your problem. Please refer bold lines.
create or replace function getNextBusinessDay(IN_DATE date)
return date
is
v_nextBusinessDate date;
v_isAHoliday boolean;
begin
v_isAHoliday := false;
v_nextBusinessDate := IN_DATE;
loop
v_isAHoliday := check_holiday(v_nextBusinessDate);
if (v_isAHoliday = false)
then
exit;
else
v_nextBusinessDate := v_nextBusinessDate+ 1;
end if;
end loop;
return v_nextBusinessDate ;
end; -
Calculating Business Days(Start_dt - End_dt)
Hi Friends,
Could anyone of you help me to find out the business days(Excluding Saturdays and Sundays) between two dates.
The difference should be calculated of both the Start_dt and End_dt values truncated. The result should be calculated and stored according to business days, it means weekend days are to be ignored.
create table sample (id number,start_dt date,end_dt date). The table values are as follows;
ID ; start_dt ; End_dt
1 ; 08-02-2012 ; 15-02-2012
2 ; 30-12-2011 ; 15-02-2012
My output should be;
ID ; start_dt ; End_dt ; Expected Business Days (Excluding Weekends)
1 ; 08-02-2012 ; 15-02-2012 ; 4
2 ; 30-12-2011 ; 15-02-2012 ; 33
Please note that, the Start Date and End Date can be any date from any year. Please help me on this.
Regards,
Williams.
Edited by: Williams on Feb 8, 2012 6:28 PM
Edited by: Williams on Feb 8, 2012 6:29 PMHere's my first cut at it.
SQL> WITH sample AS
2 (
3 SELECT 1 AS id, TO_DATE('08-02-2012','DD-MM-YYYY') AS start_dt, TO_DATE('15-02-2012','DD-MM-YYYY') AS end_dt FROM DUAL UNION ALL
4 SELECT 2 AS id, TO_DATE('30-12-2011','DD-MM-YYYY') AS start_dt, TO_DATE('15-02-2012','DD-MM-YYYY') AS end_dt FROM DUAL
5 )
6 /* END SAMPLE DATA */
7 SELECT id
8 , start_dt
9 , end_dt
10 , COUNT(*)
11 FROM sample
12 JOIN ( /* Exclude Weekends */
13 SELECT dt
14 FROM ( /* Generate a range that includes all days */
15 SELECT min_start_dt + (LEVEL - 1) AS dt
16 , TO_CHAR
17 ( min_start_dt + (LEVEL - 1)
18 , 'fmDAY'
19 , 'NLS_DATE_LANGUAGE=AMERICAN'
20 ) AS dy
21 FROM ( SELECT MIN(start_dt) AS min_start_dt
22 , MAX(end_dt) AS max_end_dt
23 FROM sample
24 )
25 CONNECT BY LEVEL <= max_end_dt - min_start_dt + 1
26 )
27 WHERE dy NOT IN ('SATURDAY','SUNDAY')
28 ) dys ON dys.dt BETWEEN start_dt AND end_dt
29 GROUP BY id
30 , start_dt
31 , end_dt
32 /
ID START_DT END_DT COUNT(*)
2 12/30/2011 00:00:00 02/15/2012 00:00:00 34
1 02/08/2012 00:00:00 02/15/2012 00:00:00 6
2 rows selected.I got different results then you though. Can you say why your counts are what they are? -
Setting Due Date for task using business days
While this may not be new news to many, I found little information on how to add a certain number of business days to determine the due date for a task to be used in a workflow. For SharePoint 2010 environments that cannot run custom code, I have
found a solution that utilizes two additional columns in the task list. The first column I named
Deadline (a number with no decimal places), which will contain the number of business days until a task is to be completed.
The second I named Business Deadline (a calculated column returning a
Date and Time). The formula for the calculated column is:
=[Modified]+INT(ROUNDDOWN([Deadline]/5,0)*7)+ IF(WEEKDAY([Modified]+MOD([Deadline],5))<MOD([Deadline],5), IF(WEEKDAY([Modified]+MOD([Deadline],5)+2)=7,MOD([Deadline],5)+4, IF(WEEKDAY([Modified]+MOD([Deadline],5)+2)=1,MOD([Deadline],5)+3,MOD([Deadline],5)+2)), IF(WEEKDAY([Modified]+MOD([Deadline],5))=7,MOD([Deadline],5)+2, IF(WEEKDAY([Modified]+MOD([Deadline],5))=1,MOD([Deadline],5)+1,MOD([Deadline],5))))
The workflow on create in the task list then is:
Set Deadline to {number of business days}
then Wait for Deadline to equal {number of business days} 'Necessary to ensure the Business Deadline has been set
'then Set Due Date to CurrentItem:Business Deadline
Hope this helps a few others that are beating their heads against their desks like I was.Hi,
Glad to hear that you solve this issue, thanks for your sharing.
Thanks,
Linda Li
Linda Li
TechNet Community Support -
Calculate business day between two dates
Hi Guys
I need the count of business days between two dates
Date1, Date2 i need the count only business day (exclude sartuday&sunday)
If date1 is null or nothing i need to pass 0
If date2 is null or nothing i need to pass 0
help on thisHi,
To achive this within SSRS, go to the report code window and add the below
Function getBusinessDaysCount(ByVal tFrom As Date, ByVal tTo As Date) As Integer
Dim tCount As Integer
Dim tProcessDate As Date = tFrom
For x as Integer= 1 To DateDiff(DateInterval.Day, tFrom, tTo) + 1
If Not (tProcessDate.DayOfWeek = DayOfWeek.Saturday Or tProcessDate.DayOfWeek = DayOfWeek.Sunday) Then
tCount = tCount + 1
End If
tProcessDate = DateAdd(DateInterval.Day, 1, tProcessDate)
Next
Return tCount
End Function
In the textbox where you need to display the value, add the below expression
=Code.getBusinessDaysCount(parameters!StartDate.Value,parameters!EndDate.Value)
It is assumed the you want to pass the two days from parameters named Startdate and EndDate. If not, modify the expression with required values.
Regards
Please click "Mark as Answer" if this resolves your problem or "Vote as Helpful" if you find it helpful. BH -
Count number of hours between dates excluding holidays/weekends
Hello all
I havent worked with dates extensively and was recently asked to create a report where Im looking for the number of hours between two dates only counting business days.
So for example I have data as follows
Created 2011-03-30 15:00:00
Processed 2011-03-30 15:03:46
Fulfilled 2011-04-01 17:25:02
Mailed 2011-04-01 17:45:00
For a total of looking from the CREATED and MAILED dates
50 hours 45 minutes
Im also trying to exclude weekends and holidays, i was reading around and was actually able to also find a table of dates where I have the main date, HOLIDAY_IND column and WEEKDAY_IND column
So the calender table i have looks similar to
CALENDER_DATE HOLIDAY_IND WEEKDAY_IND
2011-03-31 Y Y
2011-04-01 N Y
2011-04-02 N N
Im really quite stumped as to where to begin
I was thinking of trying to write it with PL/SQL but i dont have the proper user access to create procedures/functions, so looks like straight up SQL
Any help appreciated!Hi,
Depeneding on your data and your requirements, you can do something like this:
SELECT created_DATE,
, mailed_date
, 24 * ( (mailed_date - created_date)
SELECT COUNT (*)
FROM table_o_dates
WHERE main_date > created_date
AND main_date < TRUNC (mailed_date)
AND ( holiday_ind = 'Y'
OR weekday_ind = 'N'
) AS hours_between
FROM table_x
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
What do you want to do if created_date or mailed_date is not a work day? Include examples in you data and results. -
Baseline date calculation excluding non-Business days
Hi,
We have a requirement to exclude non-Business days while calculating Baseline date.
Currently the Baseline Date being populated in the Accounting Document is Document date + 5days(including Holidays).
For eg
Current Scenario
Document Date: 12th Feb 2008
Baseline date: 17th feb 2008.
Requirement:
Document Date: 12th Feb 2008
Baseline date: 19th feb 2008(Excluding Sat & Sun).
Request you to share useful info in this regard.
Thanks
BinuHi,
Use user exit RV60FUS5 for these base line date calculations.
Thanks
Krishna.
Maybe you are looking for
-
hi gurus please explain following T code in the view point of a end user V_RA BACK ORDER PROCESSING MB5B STOCK ON POSTING DATE MB5T DISPLAY STOCK IN TRANSIT VL10B PURCHASE ORDER FAST DISPLAY ME2N PURCHASIN
-
Tracking Laptops / Asset Tracking
Prey, while a software offering, has proved to be quite useful and popular in the forums.
-
How to run forms6i on Unix Machine
Hi all, I have just migrated a form4.5 to form6i. We have character mode application. Can anyone give me some idea how to run form6i on unix machine. Is it the same way we run form4.5(f45gen xyz.fmb $Path) or bit different Thanks regds Lasily Thomas
-
Error DW050 and others on installation of old PS6
What can I do when I get this error message after installing (3 times) PS6 from Creative Suite 6 Design & Web Premium: ERROR: DF024: Unable to preserve original file at "/Library/ColorSync/Profiles/Recommended" (Seq 22) ERROR: DW063: Command ARKDele
-
HT5312 dear I forget my answers of security questions and I dont have a rescue email.
dear I forget my answers of security questions and I dont have a rescue email . please help my account <Email Edited By Host>