The forum will be down for maintenance at some point between Friday, September 25, 2020 at 5pm PDT and Sunday, September 27, 2020 at 11:59 PDT. Downtime is unknown but will be minimized.

# Is there a built-in function to calculate the number of workdays between two dates?

 After checking the docs, I don't thinks so, but I might have overlooked it. To meet the requirement, I was able write a function for that purpose, based on someone else's code for Oracle. But I'd prefer a built-in solution, something like DateDiff (workday, @startdate, @enddate). asked 22 Oct '13, 10:00 Reimer Pods 4.5k●38●48●91 accept rate: 11% Mark Culp 24.4k●9●137●289

 There is no builtin procedure (function) to compute the number of workdays between two dates... but as you have already alluded to it is easy to write such a function. Example: ```create or replace function number_of_workdays( in @date1 date, in @date2 date ) returns int begin if @date1 > @date2 then -- swap dates so that date1 is before date2 begin declare @tdate date; set @tdate = @date1; set @date1 = @date2; set @date2 = @tdate; end; end if; return datediff( day, @date1, @date2 ) + 1 -- total number of days in interval (inclusive) - 2*datediff( week, @date1, @date2 ) -- minus 2*number of sundays (inc d2 but not d1) - ( if datepart( weekday, @date2 ) = 7 then 1 else 0 endif ) -- less 1 if end on Sat. - ( if datepart( weekday, @date1 ) = 1 then 1 else 0 endif ) -- less 1 if start on Sun. ; end; ``` If you are careful and don't need the date-order check in the above function then it becomes a 'one liner' which will be inlined by the SQL Anywhere execution engine. answered 22 Oct '13, 12:24 Mark Culp 24.4k●9●137●289 accept rate: 40% Thanks, Mark, that looks very much like the procedure I've created. (23 Oct '13, 08:02) Reimer Pods
 toggle preview community wiki:

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Question tags:

×34
×3

question asked: 22 Oct '13, 10:00

question was seen: 2,467 times

last updated: 23 Oct '13, 08:19