Count Days between two dates and excluding Weekends
up vote
0
down vote
favorite
How do I count days between two dates, excluding weekends?
I have this following data, I can simply getting the TAT Value by just doing ClosingDate - ActivityDate. However, I don't want to consider Saturday/Sunday.
+-------------------------+--------+-------------------------+------+
| ActivityDate | TypeID | ClosedDate | TAT |
+-------------------------+--------+-------------------------+------+
| Wednesday, May 02, 2018 | 2502 | Wednesday, May 09, 2018 | 7.00 |
| Monday, May 07, 2018 | 2503 | Thursday, May 10, 2018 | 3.00 |
| Tuesday, May 08, 2018 | 2504 | Friday, May 11, 2018 | 3.00 |
| Wednesday, May 09, 2018 | 2505 | Thursday, May 10, 2018 | 1.00 |
| Thursday, May 10, 2018 | 2506 | Friday, May 11, 2018 | 1.00 |
| Friday, May 11, 2018 | 2507 | Thursday, May 17, 2018 | 6.00 |
| Thursday, May 10, 2018 | 2508 | Tuesday, May 15, 2018 | 5.00 |
| Monday, May 14, 2018 | 2509 | Wednesday, May 16, 2018 | 2.00 |
| Monday, May 14, 2018 | 2510 | Thursday, May 17, 2018 | 3.00 |
+-------------------------+--------+-------------------------+------+
Any help is much appreciated.
sql oracle oracle11g
add a comment |
up vote
0
down vote
favorite
How do I count days between two dates, excluding weekends?
I have this following data, I can simply getting the TAT Value by just doing ClosingDate - ActivityDate. However, I don't want to consider Saturday/Sunday.
+-------------------------+--------+-------------------------+------+
| ActivityDate | TypeID | ClosedDate | TAT |
+-------------------------+--------+-------------------------+------+
| Wednesday, May 02, 2018 | 2502 | Wednesday, May 09, 2018 | 7.00 |
| Monday, May 07, 2018 | 2503 | Thursday, May 10, 2018 | 3.00 |
| Tuesday, May 08, 2018 | 2504 | Friday, May 11, 2018 | 3.00 |
| Wednesday, May 09, 2018 | 2505 | Thursday, May 10, 2018 | 1.00 |
| Thursday, May 10, 2018 | 2506 | Friday, May 11, 2018 | 1.00 |
| Friday, May 11, 2018 | 2507 | Thursday, May 17, 2018 | 6.00 |
| Thursday, May 10, 2018 | 2508 | Tuesday, May 15, 2018 | 5.00 |
| Monday, May 14, 2018 | 2509 | Wednesday, May 16, 2018 | 2.00 |
| Monday, May 14, 2018 | 2510 | Thursday, May 17, 2018 | 3.00 |
+-------------------------+--------+-------------------------+------+
Any help is much appreciated.
sql oracle oracle11g
Possible duplicate of Calculate business days in Oracle SQL(no functions or procedure)
– nop77svk
Nov 12 at 22:15
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How do I count days between two dates, excluding weekends?
I have this following data, I can simply getting the TAT Value by just doing ClosingDate - ActivityDate. However, I don't want to consider Saturday/Sunday.
+-------------------------+--------+-------------------------+------+
| ActivityDate | TypeID | ClosedDate | TAT |
+-------------------------+--------+-------------------------+------+
| Wednesday, May 02, 2018 | 2502 | Wednesday, May 09, 2018 | 7.00 |
| Monday, May 07, 2018 | 2503 | Thursday, May 10, 2018 | 3.00 |
| Tuesday, May 08, 2018 | 2504 | Friday, May 11, 2018 | 3.00 |
| Wednesday, May 09, 2018 | 2505 | Thursday, May 10, 2018 | 1.00 |
| Thursday, May 10, 2018 | 2506 | Friday, May 11, 2018 | 1.00 |
| Friday, May 11, 2018 | 2507 | Thursday, May 17, 2018 | 6.00 |
| Thursday, May 10, 2018 | 2508 | Tuesday, May 15, 2018 | 5.00 |
| Monday, May 14, 2018 | 2509 | Wednesday, May 16, 2018 | 2.00 |
| Monday, May 14, 2018 | 2510 | Thursday, May 17, 2018 | 3.00 |
+-------------------------+--------+-------------------------+------+
Any help is much appreciated.
sql oracle oracle11g
How do I count days between two dates, excluding weekends?
I have this following data, I can simply getting the TAT Value by just doing ClosingDate - ActivityDate. However, I don't want to consider Saturday/Sunday.
+-------------------------+--------+-------------------------+------+
| ActivityDate | TypeID | ClosedDate | TAT |
+-------------------------+--------+-------------------------+------+
| Wednesday, May 02, 2018 | 2502 | Wednesday, May 09, 2018 | 7.00 |
| Monday, May 07, 2018 | 2503 | Thursday, May 10, 2018 | 3.00 |
| Tuesday, May 08, 2018 | 2504 | Friday, May 11, 2018 | 3.00 |
| Wednesday, May 09, 2018 | 2505 | Thursday, May 10, 2018 | 1.00 |
| Thursday, May 10, 2018 | 2506 | Friday, May 11, 2018 | 1.00 |
| Friday, May 11, 2018 | 2507 | Thursday, May 17, 2018 | 6.00 |
| Thursday, May 10, 2018 | 2508 | Tuesday, May 15, 2018 | 5.00 |
| Monday, May 14, 2018 | 2509 | Wednesday, May 16, 2018 | 2.00 |
| Monday, May 14, 2018 | 2510 | Thursday, May 17, 2018 | 3.00 |
+-------------------------+--------+-------------------------+------+
Any help is much appreciated.
sql oracle oracle11g
sql oracle oracle11g
edited Nov 13 at 7:11
Barbaros Özhan
10.9k71430
10.9k71430
asked Nov 12 at 17:31
rocky09
417
417
Possible duplicate of Calculate business days in Oracle SQL(no functions or procedure)
– nop77svk
Nov 12 at 22:15
add a comment |
Possible duplicate of Calculate business days in Oracle SQL(no functions or procedure)
– nop77svk
Nov 12 at 22:15
Possible duplicate of Calculate business days in Oracle SQL(no functions or procedure)
– nop77svk
Nov 12 at 22:15
Possible duplicate of Calculate business days in Oracle SQL(no functions or procedure)
– nop77svk
Nov 12 at 22:15
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
How about something like this?
I took only a part of your data set. The idea is: check day number (to_char(date, 'd')); if day number of the activity date (AD) is larger than closed date (CD), then remove two additional days (weekends).
This might need to be adjusted if one (or both) of those dates fall into Saturday or Sunday (but, according to your sample data, they don't).
SQL> with test (ad, cd) as
2 (select date '2018-05-02', date '2018-05-09' from dual union all
3 select date '2018-05-11', date '2018-05-17' from dual union all
4 select date '2018-05-10', date '2018-05-15' from dual
5 ),
6 day_num as
7 (select ad, cd,
8 to_char(ad, 'd') addn, to_char(cd, 'd') cddn,
9 to_char(ad, 'dy') ady, to_char(cd, 'dy') cdy
10 from test
11 )
12 select ad, cd,
13 cd - ad - case when addn > cddn then 2 else 0 end diff
14 from day_num;
AD CD DIFF
--------------- --------------- ----------
02.05.2018, wed 09.05.2018, wed 7
11.05.2018, fri 17.05.2018, thu 4
10.05.2018, thu 15.05.2018, tue 3
SQL>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
How about something like this?
I took only a part of your data set. The idea is: check day number (to_char(date, 'd')); if day number of the activity date (AD) is larger than closed date (CD), then remove two additional days (weekends).
This might need to be adjusted if one (or both) of those dates fall into Saturday or Sunday (but, according to your sample data, they don't).
SQL> with test (ad, cd) as
2 (select date '2018-05-02', date '2018-05-09' from dual union all
3 select date '2018-05-11', date '2018-05-17' from dual union all
4 select date '2018-05-10', date '2018-05-15' from dual
5 ),
6 day_num as
7 (select ad, cd,
8 to_char(ad, 'd') addn, to_char(cd, 'd') cddn,
9 to_char(ad, 'dy') ady, to_char(cd, 'dy') cdy
10 from test
11 )
12 select ad, cd,
13 cd - ad - case when addn > cddn then 2 else 0 end diff
14 from day_num;
AD CD DIFF
--------------- --------------- ----------
02.05.2018, wed 09.05.2018, wed 7
11.05.2018, fri 17.05.2018, thu 4
10.05.2018, thu 15.05.2018, tue 3
SQL>
add a comment |
up vote
0
down vote
How about something like this?
I took only a part of your data set. The idea is: check day number (to_char(date, 'd')); if day number of the activity date (AD) is larger than closed date (CD), then remove two additional days (weekends).
This might need to be adjusted if one (or both) of those dates fall into Saturday or Sunday (but, according to your sample data, they don't).
SQL> with test (ad, cd) as
2 (select date '2018-05-02', date '2018-05-09' from dual union all
3 select date '2018-05-11', date '2018-05-17' from dual union all
4 select date '2018-05-10', date '2018-05-15' from dual
5 ),
6 day_num as
7 (select ad, cd,
8 to_char(ad, 'd') addn, to_char(cd, 'd') cddn,
9 to_char(ad, 'dy') ady, to_char(cd, 'dy') cdy
10 from test
11 )
12 select ad, cd,
13 cd - ad - case when addn > cddn then 2 else 0 end diff
14 from day_num;
AD CD DIFF
--------------- --------------- ----------
02.05.2018, wed 09.05.2018, wed 7
11.05.2018, fri 17.05.2018, thu 4
10.05.2018, thu 15.05.2018, tue 3
SQL>
add a comment |
up vote
0
down vote
up vote
0
down vote
How about something like this?
I took only a part of your data set. The idea is: check day number (to_char(date, 'd')); if day number of the activity date (AD) is larger than closed date (CD), then remove two additional days (weekends).
This might need to be adjusted if one (or both) of those dates fall into Saturday or Sunday (but, according to your sample data, they don't).
SQL> with test (ad, cd) as
2 (select date '2018-05-02', date '2018-05-09' from dual union all
3 select date '2018-05-11', date '2018-05-17' from dual union all
4 select date '2018-05-10', date '2018-05-15' from dual
5 ),
6 day_num as
7 (select ad, cd,
8 to_char(ad, 'd') addn, to_char(cd, 'd') cddn,
9 to_char(ad, 'dy') ady, to_char(cd, 'dy') cdy
10 from test
11 )
12 select ad, cd,
13 cd - ad - case when addn > cddn then 2 else 0 end diff
14 from day_num;
AD CD DIFF
--------------- --------------- ----------
02.05.2018, wed 09.05.2018, wed 7
11.05.2018, fri 17.05.2018, thu 4
10.05.2018, thu 15.05.2018, tue 3
SQL>
How about something like this?
I took only a part of your data set. The idea is: check day number (to_char(date, 'd')); if day number of the activity date (AD) is larger than closed date (CD), then remove two additional days (weekends).
This might need to be adjusted if one (or both) of those dates fall into Saturday or Sunday (but, according to your sample data, they don't).
SQL> with test (ad, cd) as
2 (select date '2018-05-02', date '2018-05-09' from dual union all
3 select date '2018-05-11', date '2018-05-17' from dual union all
4 select date '2018-05-10', date '2018-05-15' from dual
5 ),
6 day_num as
7 (select ad, cd,
8 to_char(ad, 'd') addn, to_char(cd, 'd') cddn,
9 to_char(ad, 'dy') ady, to_char(cd, 'dy') cdy
10 from test
11 )
12 select ad, cd,
13 cd - ad - case when addn > cddn then 2 else 0 end diff
14 from day_num;
AD CD DIFF
--------------- --------------- ----------
02.05.2018, wed 09.05.2018, wed 7
11.05.2018, fri 17.05.2018, thu 4
10.05.2018, thu 15.05.2018, tue 3
SQL>
answered Nov 13 at 5:55
Littlefoot
18k51333
18k51333
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267266%2fcount-days-between-two-dates-and-excluding-weekends%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Calculate business days in Oracle SQL(no functions or procedure)
– nop77svk
Nov 12 at 22:15