Assuming that in your system, Saturday is day 7 and Sunday is day 1, the following formula will give you days between two dates, excluding weekends (pseudo-code, rather than PL/SQL, for readability):

iTotalDay = int(toDate) - int(fromDate); -- get the integer total days between iWeekDays = iTotalDay - (int((iTotalDay-1)/7)*2) -- subtract all the included weekend days -- now we just need to handle the start/end days if DayOfWeek(fromDate) = Sunday then iWeekDays = iWeekDays - 1 elseif DayOfWeek(fromDate) = Saturday then iWeekDays = iWeekDays - 2 end if if DayOfWeek(toDate) = Sunday then iWeekDays = iWeekDays - 2 elseif DayOfWeek(toDate) = Saturday then iWeekDays = iWeekDays - 1 end if

There are some special cases that can all be handled by testing the end result for a negative number – when negative, the correct answer is 0. This can occur when the period is too short and includes 1-2 weekend days. (e.g., What is the count of days between saturday and sunday, not counting weekend days?).

if iWeekDays < 0 then iWeekDays = 0 end if]]>

and day of week of ending date

and for every 7 days between them subtract 2.

You would have to develop a process when the original difference is < 7.

You would have to develop a process for when one of the dates is itself on a week-end.

Another approach to this sort thing is to have a “dates table” that includes not only days of the week (so you can find week-ends) but also holidays and other dates that might not be “business days.”

]]>Now I have an advanced question on this topic. How can I get the days between two dates without weekends?

Best regards,

Philipp

If Oracle knows it’s a date format, it can make the comparison

with just an ordinary compare like this:

`date_to - date_from`

select to_date('2006-10-06', 'yyyy-mm-dd') - to_date('2006-09-22', 'yyyy-mm-dd') from dual;

`TO_DATE('2006-10-06','YYYY-MM-DD')-TO_DATE('2006-09-22','YYYY-MM-DD')`

———————————————————————

14

/Maria

]]>`SELECT date1 - date2 as "number of days"`

FROM orders;

Should do the trick. ]]>

It’s worth googling “days_between”, asktom may be useful …

http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:6904756630649

and the latest docco is at:

http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements001.htm#i78291

HTH, Colin.

]]>`select to_date('2006-10-06`

','yyyy-mm-dd') - to_date('2006-09-22

','yyyy-mm-dd')from dual;

]]>