| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Mailing Lists -> Oracle-L -> Re: How to calculate the working hours?
This is a multi-part message in MIME format.
------=_NextPart_000_002C_01C2EDFC.8EAE8A20 Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
RE: How to calculate the working hours?What about holydays?
Igor Neyman, OCP DBA
ineyman_at_perceptron.com
=20
see answer below=20
> -----Original Message-----=20
> From: Andrea Oracle [mailto:andreaoracle_at_yahoo.com]=20
>=20
>=20
>=20
I couldn't resist the challenge. The formula below is clunky but it =
works.=20
Assume two dates, d1 and d2, with d1 <=3D d2. Find the number of =
working hours between d1 and d2.=20
Oracle SQL formula:=20
(&&end_hour - &&start_hour) =
=20
* (trunc (decode (to_number (to_char (d2, 'D')), &&saturday, d2 - 1, =
&&sunday, d2 - 2, d2)) =20
- trunc (decode (to_number (to_char (d1, 'D')), &&saturday, d1 - =
1, &&sunday, d1 - 2, d1)) =20
) =
=20
- 2 * (&&end_hour - &&start_hour) =
=20
* (floor ((trunc (d2) - trunc (d1)) / 7) =
=20
+ decode (sign (to_number (to_char (d2, 'D')) - to_number =
(to_char (d1, 'D'))), =20
-1, 1, 0 =
=20
) =
=20
) =
=20
+ (decode (to_number (to_char (d2, 'D')), =
=20
&&saturday, &&end_seconds, =
=20
&&sunday, &&end_seconds, =
=20
greatest (least (to_number (to_char (d2, 'SSSSS')) - =
&&start_seconds, &&end_seconds), 0) =20
) =
=20
- decode (to_number (to_char (d1, 'D')), =
=20
&&saturday, &&end_seconds, =
=20
&&sunday, &&end_seconds, =
=20
greatest (least (to_number (to_char (d1, 'SSSSS')) - =
&&start_seconds, &&end_seconds), 0)=20
) =
=20
) / 3600 =
=20
=
=20
where=20
&&saturday is to_date (..., 'D') for saturday (will depend on your =
NLS_TERRITORY setting)=20
&&sunday is to_date (..., 'D') for sunday (will depend on your =
NLS_TERRITORY setting)=20
&&start_hour is start of workday (in your case 8:00)=20 &&end_hour is end of workday (in your case 17:00)=20 &&start_seconds is number of seconds from midnight to &&start_hour=20 &&end_seconds is number of seconds between &&start_hour and &&end_hour =
Proof of concept for your specifications (workday from 8:00 to 17:00)=20
------=_NextPart_000_002C_01C2EDFC.8EAE8A20 Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>RE: How to calculate the working hours?</TITLE> <META http-equiv=3DContent-Type content=3D"text/html; =charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4731.2200" name=3DGENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=3D#ffffff> <DIV><FONT size=3D2>What about holydays?</FONT></DIV> <DIV> </DIV> <DIV>Igor Neyman, OCP DBA<BR><A=20
<DIV> </DIV> <DIV> </DIV> <BLOCKQUOTE dir=3Dltr=20
<DIV><BR></DIV> <P><FONT size=3D2>see answer below</FONT> </P> <P><FONT size=3D2>> -----Original Message-----</FONT> <BR><FONT =size=3D2>>=20
Time</FONT> <BR><FONT size=3D2>> ------------------</FONT> =<BR><FONT=20
size=3D2>> 03/12/03 11:08:07</FONT> <BR><FONT size=3D2>> = </FONT><BR><FONT=20 size=3D2>> How to calculate the working hours (8am - 5pm, no</FONT> =<BR><FONT=20
&&start_hour) &nbs= p;  = ; = &= nbsp; &n= bsp; &nb=sp; =20
) = &= nbsp; &n= bsp; &nb= sp; &nbs= p;  = ; = =20 </FONT><BR><FONT size=3D2>- 2 * (&&end_hour -=20 =
&&start_hour) &nbs= p;  = ; = &= nbsp; &n=bsp; =20 </FONT><BR><FONT size=3D2> * (floor ((trunc (d2) - = trunc (d1))=20
7)  = ; = &= nbsp; &n=bsp; =20 </FONT><BR><FONT size=3D2> + = decode=20
0 = &= nbsp; &n= bsp; &nb= sp; &nbs= p;  =; =20
) = &= nbsp; &n= bsp; &nb= sp; &nbs= p;  =; =20 </FONT><BR><FONT size=3D2> =20 =
) = &= nbsp; &n= bsp; &nb= sp; &nbs= p;  = ; = =20 </FONT><BR><FONT size=3D2>+ (decode (to_number (to_char (d2,=20 =
'D')), &= nbsp; &n= bsp; &nb= sp; &nbs=p; =20 </FONT><BR><FONT=20
&&end_seconds, &nb= sp; &nbs= p;  = ; = &=nbsp; =20
&&end_seconds, &nb= sp; &nbs= p;  = ; = &=nbsp; =20 </FONT><BR><FONT=20
) = &= nbsp; &n= bsp; &nb= sp; &nbs= p;  = ; = =20
'D')), &= nbsp; &n= bsp; &nb= sp; &nbs=p; =20 </FONT><BR><FONT=20
&&end_seconds, &nb= sp; &nbs= p;  = ; = &=nbsp; =20
&&end_seconds, &nb= sp; &nbs= p;  = ; = &=nbsp; =20
) = &= nbsp; &n= bsp; &nb= sp; &nbs= p;  = ; = =20
3600 &nb= sp; &nbs= p;  = ; = &= nbsp; &n= bsp; &nb=sp; =20
size=3D2> &nbs= p;  = ; = &= nbsp; &n= bsp; &nb= sp; &nbs= p;  =; =20
<BR><FONT size=3D2> + (decode (to_number (to_char (d2, = 'D')),</FONT>=20 <BR><FONT=20
![]() |
![]() |