Please be aware that the content in SAP SQL Anywhere Forum will be migrated to the SAP Community in June and this forum will be retired.

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%20Pods's gravatar image

Reimer Pods
accept rate: 11%

edited 22 Oct '13, 10:31

Mark%20Culp's gravatar image

Mark Culp

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
  if @date1 > @date2 then
    -- swap dates so that date1 is before date2
      declare @tdate date;
      set @tdate = @date1;
      set @date1 = @date2;
      set @date2 = @tdate;
  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.

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.

permanent link

answered 22 Oct '13, 12:24

Mark%20Culp's gravatar image

Mark Culp
accept rate: 41%

Thanks, Mark, that looks very much like the procedure I've created.

(23 Oct '13, 08:02) Reimer Pods
Your answer
toggle preview

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • 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:


question asked: 22 Oct '13, 10:00

question was seen: 3,186 times

last updated: 23 Oct '13, 08:19