Calculate: SYSDATE + TWO BUSINESS DAYS
Is there a simple way (function) in 10g to calculate
- "SYSDATE + TWO BUSINESS DAYS (not including Saturday)"
- "SYSDATE + TWO BUSINESS DAYS (including Saturday)"
in SQL ? Or I have to write a function to calculate manually?
Thank you for any idea.
One possible solution, based on a table of holidays:
CREATE TABLE HOLIDAY_TABLE
(HOLIDAY_DATE DATE PRIMARY KEY,
DESCRIPTION VARCHAR2(50));
SELECT dt
FROM (SELECT dt, RANK () OVER (ORDER BY dt) pos
FROM (SELECT TRUNC (SYSDATE) + LEVEL dt,
CASE
WHEN TRIM (TO_CHAR (SYSDATE + LEVEL, 'DAY')) = 'SATURDAY'
OR TRIM (TO_CHAR (SYSDATE + LEVEL, 'DAY')) = 'SUNDAY'
THEN 0
ELSE 1
END cnt
FROM DUAL
CONNECT BY LEVEL < 7) -- number large enough to generate a complete list of possible days
WHERE cnt = 1 AND (SELECT COUNT (*)
FROM holiday_table
WHERE holiday_date = dt) = 0)
WHERE pos = 2 -- number of working days to add Miguel
Similar Messages
-
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 -
How to calculate previous business day
Hi,
I'm stuck with an issue.
Task is to calculate 1 to 6 previous business days with reference to system date.
Conditions are : Week is 6 day. In first case its Sunday as weekend holiday and in second case its Saturday as weekend holiday.
Another condition is I have to take care of bank holidays also which are stored in a separate table in database.
How to implement this logic ?
Below I have implemented some logic but its not running correctly for some dates adjusted according to bank holidays.
In this code I'm taking Sunday as weekend off and To_Date is having the list of bank holidays for 2012.
SELECT
CASE
WHEN TRUNC(SYSDATE-1) IN (TO_DATE('01-JAN-2012'), TO_DATE('02-JAN-2012'), TO_DATE('07-APR-2012'), TO_DATE('09-APR-2012'), TO_DATE('07-MAY-2012'), TO_DATE('04-JUN-2012'), TO_DATE('05-JUN-2012'), TO_DATE('27-AUG-2012'), TO_DATE('25-DEC-2012'), TO_DATE('26-DEC-2012'))
THEN (
CASE
WHEN TRUNC(SYSDATE-2) IN (TO_DATE('01-JAN-2012'), TO_DATE('02-JAN-2012'), TO_DATE('07-APR-2012'), TO_DATE('09-APR-2012'), TO_DATE('07-MAY-2012'), TO_DATE('04-JUN-2012'), TO_DATE('05-JUN-2012'), TO_DATE('27-AUG-2012'), TO_DATE('25-DEC-2012'), TO_DATE('26-DEC-2012'))
Then (
CASE
WHEN TRUNC(SYSDATE-3) IN (TO_DATE('01-JAN-2012'), TO_DATE('02-JAN-2012'), TO_DATE('07-APR-2012'), TO_DATE('09-APR-2012'), TO_DATE('07-MAY-2012'), TO_DATE('04-JUN-2012'), TO_DATE('05-JUN-2012'), TO_DATE('27-AUG-2012'), TO_DATE('25-DEC-2012'), TO_DATE('26-DEC-2012'))
Then (
CASE
WHEN TRUNC(SYSDATE-4) IN (TO_DATE('01-JAN-2012'), TO_DATE('02-JAN-2012'), TO_DATE('07-APR-2012'), TO_DATE('09-APR-2012'), TO_DATE('07-MAY-2012'), TO_DATE('04-JUN-2012'), TO_DATE('05-JUN-2012'), TO_DATE('27-AUG-2012'), TO_DATE('25-DEC-2012'), TO_DATE('26-DEC-2012'))
Then (
CASE
WHEN TRUNC(SYSDATE-5) IN (TO_DATE('01-JAN-2012'), TO_DATE('02-JAN-2012'), TO_DATE('07-APR-2012'), TO_DATE('09-APR-2012'), TO_DATE('07-MAY-2012'), TO_DATE('04-JUN-2012'), TO_DATE('05-JUN-2012'), TO_DATE('27-AUG-2012'), TO_DATE('25-DEC-2012'), TO_DATE('26-DEC-2012'))
Then (
CASE
WHEN TRUNC(SYSDATE-6) IN (TO_DATE('01-JAN-2012'), TO_DATE('02-JAN-2012'), TO_DATE('07-APR-2012'), TO_DATE('09-APR-2012'), TO_DATE('07-MAY-2012'), TO_DATE('04-JUN-2012'), TO_DATE('05-JUN-2012'), TO_DATE('27-AUG-2012'), TO_DATE('25-DEC-2012'), TO_DATE('26-DEC-2012'))
THEN DECODE(TO_CHAR((SYSDATE-6),'DAY'),'MONDAY ',TRUNC((SYSDATE-6)-7),'TUESDAY ',TRUNC((SYSDATE-6) - 7),'WEDNESDAY',TRUNC((SYSDATE-6) - 7),'THURSDAY ',TRUNC((SYSDATE-6) - 7),'FRIDAY ',TRUNC((SYSDATE-6) - 7),'SATURDAY ',TRUNC((SYSDATE-6) - 7),'SUNDAY ',TRUNC((SYSDATE-6) - 6))
ELSE DECODE(TO_CHAR((SYSDATE-5),'DAY'),'MONDAY ',TRUNC((SYSDATE-5)-7),'TUESDAY ',TRUNC((SYSDATE-5) - 7),'WEDNESDAY',TRUNC((SYSDATE-5) - 7),'THURSDAY ',TRUNC((SYSDATE-5) - 7),'FRIDAY ',TRUNC((SYSDATE-5) - 7),'SATURDAY ',TRUNC((SYSDATE-5) - 7),'SUNDAY ',TRUNC((SYSDATE-5) - 6))
END)
ELSE DECODE(TO_CHAR((SYSDATE-4),'DAY'),'MONDAY ',TRUNC((SYSDATE-4)-7),'TUESDAY ',TRUNC((SYSDATE-4) - 7),'WEDNESDAY',TRUNC((SYSDATE-4) - 7),'THURSDAY ',TRUNC((SYSDATE-4) - 7),'FRIDAY ',TRUNC((SYSDATE-4) - 7),'SATURDAY ',TRUNC((SYSDATE-4) - 7),'SUNDAY ',TRUNC((SYSDATE-4) - 6))
END)
ELSE DECODE(TO_CHAR((SYSDATE-3),'DAY'),'MONDAY ',TRUNC((SYSDATE-3)-7),'TUESDAY ',TRUNC((SYSDATE-3) - 7),'WEDNESDAY',TRUNC((SYSDATE-3) - 7),'THURSDAY ',TRUNC((SYSDATE-3) - 7),'FRIDAY ',TRUNC((SYSDATE-3) - 7),'SATURDAY ',TRUNC((SYSDATE-3) - 7),'SUNDAY ',TRUNC((SYSDATE-3) - 6))
END)
ELSE DECODE(TO_CHAR((SYSDATE-2),'DAY'),'MONDAY ',TRUNC((SYSDATE-2)-7),'TUESDAY ',TRUNC((SYSDATE-2) - 7),'WEDNESDAY',TRUNC((SYSDATE-2) - 7),'THURSDAY ',TRUNC((SYSDATE-2) - 7),'FRIDAY ',TRUNC((SYSDATE-2) - 7),'SATURDAY ',TRUNC((SYSDATE-2) - 7),'SUNDAY ',TRUNC((SYSDATE-2) - 6))
END )
ELSE DECODE(TO_CHAR((SYSDATE-1),'DAY'),'MONDAY ',TRUNC((SYSDATE-1)-7),'TUESDAY ',TRUNC((SYSDATE-1) - 7),'WEDNESDAY',TRUNC((SYSDATE-1) - 7),'THURSDAY ',TRUNC((SYSDATE-1) - 7),'FRIDAY ',TRUNC((SYSDATE-1) - 7),'SATURDAY ',TRUNC((SYSDATE-1) - 7),'SUNDAY ',TRUNC((SYSDATE-1) - 6))
END)
ELSE DECODE(TO_CHAR((SYSDATE),'DAY'),'MONDAY ',TRUNC((SYSDATE)-7),'TUESDAY ',TRUNC((SYSDATE) - 7),'WEDNESDAY',TRUNC((SYSDATE) - 7),'THURSDAY ',TRUNC((SYSDATE) - 7),'FRIDAY ',TRUNC((SYSDATE) - 7),'SATURDAY ',TRUNC((SYSDATE) - 7),'SUNDAY ',TRUNC((SYSDATE) - 6))
END
FROM DUAL;Edited by: user9082359 on Oct 21, 2012 8:55 PMHello,
There are two business streams.
For one stream,Saturday is holiday and for other Sunday is holiday and
('01-JAN-2012'), ('02-JAN-2012'), ('07-APR-2012'), ('09-APR-2012'), ('07-MAY-2012'), ('04-JUN-2012'), ('05-JUN-2012'), ('27-AUG-2012'), ('25-DEC-2012'), ('26-DEC-2012') is my list of bank holidays for 2012 year.
Now if suppose we have to calculate 1 previous business day for today 22 Oct,it would be 20 Oct for one stream and 21 Oct for another stream.
Like this I have to calculate 1-6 previous business days for sysdate.
Now If there is a bank holiday coming in between e.g. suppose 20 Oct is a bank holiday and Sunday is weekend holiday,so now 1 previous business day would be 19 Oct.
I hope I have made myself clear.
Thanks and waiting for solution of this issue. -
Previous business day in sql query
Hi,
Is there a way to calculate the previous business day on teh sql query without using a function call.
Typically in the query where clause as seen below:
eg)
select * from sales where sale_date ='+previous_buss_day+'
If so please let me know....Thnx in advanc.that depends if your organization has a defined set of business days that will includes non-working days (holidays, etc.). if there is you need to have a lookup table that has a listing of all available normal business day and non-working days. then match it up with your transaction table. otherwise you can use the current system date less 1 days.
e.g.
select * from sales where sale_date = trunc(sysdate-1) -
Calculation of Business day in OBIEE
Hi,
How to calculate the business day in OBIEE by excluding both weekends and public holidays.
Please guide meIf you want the "nth business day," here is something I prepared.
Understanding the Concept in Layman Terms
The first step to deriving the calculation is looking at a calendar. For our example, we will calculate the 6th business day of each month.
1) Observation One: When the first day of the month is Saturday, the 6th business day occurs when “today’s date minus the first-day-of-the month equals 9.”
2) Observation Two: When the first day of the month is Sunday, the 6th business day occurs when the above calculation equals 8.
3) Observation Three: When the first day of the month is Monday – Friday, the 6th business day occurs when the above calculation equals 7.
Building the SQL
Below are the SQL statements that correspond to the above observations:
4) When the first day of the month is Saturday…
DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE)) = 6 AND DAYOFMONTH(CURRENT_DATE)-DAYOFMONTH(TIMESTAMPADD(SQL_TSI_DAY,1,TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE))) = 9
5) When the first day of the month is Sunday…
DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE)) = 7 AND DAYOFMONTH(CURRENT_DATE)-DAYOFMONTH(TIMESTAMPADD(SQL_TSI_DAY,1,TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE))) = 8
6) When the first day of the month is Monday – Friday…
DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE)) NOT IN (6,7) AND DAYOFMONTH(CURRENT_DATE)-DAYOFMONTH(TIMESTAMPADD(SQL_TSI_DAY,1,TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE))) = 7
Putting the SQL Statements Together
When any of the above conditions are true (“OR” between each condition), then (THEN) “today” is the 6TH business day; otherwise (ELSE) it is not the 6th business day. Below is the final SQL:
CASE WHEN
DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE)) = 6 AND DAYOFMONTH(CURRENT_DATE)-DAYOFMONTH(TIMESTAMPADD(SQL_TSI_DAY,1,TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE))) = *9*
OR
DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE)) = 7 AND DAYOFMONTH(CURRENT_DATE)-DAYOFMONTH(TIMESTAMPADD(SQL_TSI_DAY,1,TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE))) = *8*
OR
DAYOFWEEK(TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE)) IN (1,2,3,4,5) AND DAYOFMONTH(CURRENT_DATE)-DAYOFMONTH(TIMESTAMPADD(SQL_TSI_DAY,1,TIMESTAMPADD(SQL_TSI_DAY,DAYOFMONTH(CURRENT_DATE)*-1,CURRENT_DATE))) = *7*
THEN '6th Business Day' ELSE 'Not 6th Business Day' END
Modifying the Query for the nth Business Day
To modify the above SQL for the nth business day, change the numbers in bold to the appropriate values. -
Help with expression for business days
Dear Frends,
I need help with business days expression.
The report is for call center operation. Whenever a user calls in and opens an issue it is recorded as a case. And each case is associated with create time and valueofone(which is always one).
My report contains 'case type' as the first column, and rest of the columns are age buckets(col1: 0-5 days, col2: 6-10 days). By saying agebuckets I mean number of days between create date and current date. All the cases that are less than 5 days older will go to first column and similarly all the cases that are 6-10 days old will go to the second column and so on.
In a normal case I use the following case statement: case when daysbetween(current_date, create_date) between x1 and x2 then count(valueofone) else null end
But I need to calculate only the business days as age, so I need an expression look something like this: case when (_days_between(current_date, create_date)-no of holidays) between x1 and x2 then count(valueofone) else null end
Note: My calendar table has a column 'Business Day' which assigns 'Y' to Business day and 'N' to holiday.
Any help with this expression or any suitable way.
Regards
ChillaIf you have a calendar table, wouldn't your function be just
CREATE FUNCTION business_days_between( p_day1 IN DATE, p_day2 IN DATE )
RETURN NUMBER
IS
l_cnt NUMBER;
BEGIN
SELECT COUNT(*)
INTO l_cnt
FROM calendar_table ct
WHERE ct.business_day = 'Y'
AND ct.<<date column>> >= trunc(p_day1)
AND ct.<<date column>> < trunc(p_day2);
RETURN l_cnt;
END;Basially, count the number of rows in your calendar table that are business days between the two dates.
Justin -
Hi All,
I need to find the calendar date 30 business days back from SYSDATE:
Formula: Calendar Date = SYSDATE - "30 business days"
I have a table contains:
cal_dt,bus_day,holiday
7/1/10,1,NULL
7/2/10,1,NULL
7/3/10,0,NULL
7/4/10,0,NULL
7/5/10,0,'Y'
7/6/10,1,NULL
For the life of me, I can't figure it out how to write the SQL? Please help.
Using Oracle 10g.
Thanks,
MaryThe first query gives you: Today's date - 30 'BUSINESS' days
select cal_dt
from
select cal_dt, bus_day, holiday,
row_number() over (order by cal_dt desc) rn
from calendar
where cal_dt <= TRUNC(SYSDATE)
and bus_day = 1
and holiday is NULL
where rn = 31;
The second query gives you: Last 'BUSINESS' day preceding (Today's date - 30 'CALENDER' days)
Select cal_dt
from (
select cal_dt, bus_day, holiday,
row_number() over (order by cal_dt desc) rn
from calendar
where cal_dt <= TRUNC(SYSDATE-30)
and bus_day = 1
and holiday is NULL
where rn = 1;
Or, you may try a simple alternative to my first query:
select cal_dt
from
select cal_dt, ROWNUM rn
from
select cal_dt, bus_day, holiday
from calendar
where cal_dt <= TRUNC(SYSDATE)
and bus_day = 1
and holiday is NULL
order by cal_dt
where rn = 31;
You may try to run these queries step by step to check what is happening and what suits to your needs. -
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) -
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 -
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 -
Formula to calculate due date considering only business days (Middle East)
I have a form which contains two fields: 'Start Date' and 'Completion Date'; the 'Completion Date' field is automatically set, it's always 3 more days than the 'Start Date' field.
The problem is that these 3 days must be only business days.
- if 'Start Date' is on Sunday then the 'Completion Date' should be set to next Wednesday
- if 'Start Date' is on Monday then the 'Completion Date' should be set to next Friday
- if 'Start Date' is on Tuesday then the 'Completion Date' should be set to next Sunday
- if 'Start Date' is on Wednesday then the 'Completion Date' should be set to next Monday
- if 'Start Date' is on Thursday then the 'Completion Date' should be set to next Tuesday
- if 'Start Date' is on Friday then the 'Completion Date' should be set to next Tuesday
- if 'Start Date' is on Saturday then the 'Completion Date' should be set to next Tuesday
Friday and Saturday should be ignored as 'Completion Date' (weekend in Middle East).
Anyone has any idea of how to make this formula? I need to put it into a calculated SharePoint column.
Thanks in advance for your help.Hi
check my post
http://www.romeodonca.ro/Lists/DB_IT/Adding%20a%20number%20of%20working%20days%20to%20a%20specific%20date
If you need more details let me know
Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you. -
I wanted to know how do you calculate the number of days between two dates
i wanted to know how do you calculate the number of days between two dates in java ? i get both the dates from the database. i guess there are many issues like leap year and Febuary having diff no of months ..etc.
thanks..
I solve my problem as
public class MyExample {
public static void main(String a[]) {
String stdate = "2009-03-01";
java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
java.sql.Date preDate = java.sql.Date.valueOf(stdate);
System.out.println(currentDate);
System.out.println(preDate);
// int dateCom = preDate.compareTo(currentDate);
// System.out.println(dateCom);
long diff = currentDate.getTime() - preDate.getTime();
int days = (int) Math.floor(diff / (24 * 60 * 60 * 1000));
System.out.println(days);
} -
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. -
Formula to calculate due date considering only business days
Hello,
I have a form which contains two fields: 'Start Date' and 'Due Date'. The 'Due Date' field is automatically set , it's always 4 more days than the 'Start Date' field.
The problem is: Those 4 days must be only business days... e.g., if 'Start Date' is on Friday then the 'Due Date' should be set on the next Thursday. Saturday and Sunday are ignored. Anyone has any idea of how to make this formula? I need to put it into
a calculated SharePoint column.
Thanks in advance.Hello,
you can use this calculated formula for Due Date:
=IF(WEEKDAY(StartDate+4)=7,StartDate+6,IF(WEEKDAY(StartDate+4)=1,StartDate+5,StartDate+4))
Be aware that it treats holidays and national bank holidays as business days unless they are on a weekend.
cheers, teylyn
teylyn,
Thank you for your example. However this formula doesn't seem to be entirely correct, right? Because
according to the excel spreadsheet you sent, when the Start Date is May 29th (Thursday), Due Date
will be on June 2nd (Monday) taking so only two days rather than four.
Maybe a little change in this formula and we will be fine. -
Hi,
Iam working on a logic to calculate the posting date in XI. I have used graphical mapping in the scenario.
The logic is to claculate the 3rd business date of the month. You need to look at the calendar and exclude Saturdays, Sundays and Holidays.
For example if we take February 2008 the 3rd business day is 5th Feb 08 as 02/Feb and 03/Feb are saturday and sundays.
If monday(04/Feb) was a holiday then the 3rd business day will be 6th Feb.
Can you give this logic in XI graphical mapping.
Best Regards
SureshHi Ram,
Thanks for the immediate reply. The XI scenario is File to IDOC. But the scenario is already developed using graphical mapping and I just need to change the posting date logic in it. I believe the holidays are maintained in SAP tables. Is there a way to get the holidays list from SAP using User defined functions in XI.
Best Regards
Suresh
Maybe you are looking for
-
Macbook performance without the battery
I use the macbook pro for editing video in Final cut pro, and sometimes when I am at home I remove the battery. and then I choose better performance, but the performance is not so good, i have constantly drop frames warning in the Final Cut. I've rea
-
i upgraded my mbp to 10.6.2 and i dunno why but it is automatically changing set up for my dock to hide and show setting what do i do?
-
Sluggish iMac performance; maintenance help needed
I have a 21.5 inch, mid-2011 iMac with 12 GB memory and a 1 Tb drive (660 GB free). Have upgraded to Mavericks, and the iMac is running slow (although it was sluggish prior to the upgrade). Am looking for advice on how to get things sped up a bit. I
-
Intercompany billing IDOc issue
Hi, I am intercomapny purchase billing documents and intercomapny sales billing documents thropugh IDOC posting to generate automatic invocies. All IDOCs for intercomapny purchase billing documents are going into error. Error is : Enter a tax code in
-
Budget posting mi32 document how to view
Hi All, Budget posting done in IM32 and IM52 posted document is genreated but how to view the document Number in system. which t code. Regards, juhi