meta data for this page
  •  

F_COUNTWEEKDAYS

function from adhoc

Entrypoint countweekdays compatible with UTF-8


Inputs/Outputs

   Input      TIMESTAMP1     date optionally time 1
              TIMESTAMP2     date optionally time 2
              INTEGER        No. of weekday
   Output     INTEGER        number of chosen weekdays between date 1 and date 2 

Syntax

   Monday = 1, Thuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6, Sunday = 7
   Counts number of choosen weekdays between date 1 and date 2 
   Starting with version adhoc20090128
   - and order of param 1 and 2 (timestamp 1 must not be older than timestamp 2)
   - for all weekdays index < 1 and > 7 return is always 0
   TestSQL
   SELECT 4 AS ISCORRECT, F_COUNTWEEKDAYS('04.02.2008', '27.02.2008', 2) FROM RDB$DATABASE;
   SELECT 4 AS ISCORRECT, F_COUNTWEEKDAYS('05.02.2008', '26.02.2008', 2) FROM RDB$DATABASE;
   SELECT 3 AS ISCORRECT, F_COUNTWEEKDAYS('06.02.2008', '27.02.2008', 2) FROM RDB$DATABASE;
   SELECT 1 AS ISCORRECT, F_COUNTWEEKDAYS('12.12.2008', '17.12.2008', 6) FROM RDB$DATABASE;
   SELECT 1 AS ISCORRECT, F_COUNTWEEKDAYS('17.12.2008', '12.12.2008', 6) FROM RDB$DATABASE;
   SELECT 0 AS ISCORRECT, F_COUNTWEEKDAYS('12.12.2008', '17.12.2008', 9) FROM RDB$DATABASE;
   SELECT 0 AS ISCORRECT, F_COUNTWEEKDAYS('12.12.2008', '17.12.2008', -5) FROM RDB$DATABASE;
   SELECT NULL AS ISCORRECT, F_COUNTWEEKDAYS(NULL, NULL, NULL) FROM RDB$DATABASE;

Preliminary Note

   Preliminary note to counting the week of the year - calendar week:
   The year contains minimum 52 serial weeks (calendar weeks). There are different versions to count. The first week of the year is the week
       * which contains the 1st January (USA, Excel function)
       * The first week which contains minimum 4 days of the new year (DIN 1355 / ISO 8601)
       * The first complete week of the year (seldom)
   The international norm ISO 8601 (1973) appoints Monday as starting day of the week. Since 1976 Monday is the first day of the week in Germany (DIN 1355). In 1978 the UNO decided, that Monday is the first day of the week.
   The results of this are the following rules:
       * every Monday and only on Monday a new calendar week starts
       * the first calendar week of the year is the week that contains minimum 4 days of the new year
   The results of these rules are the following characteristics:
       * There are no uncomplete calendar weeks, without fail every week contains exactly 7 days
       * every year contains 52 or 53 calendar weeks
       * if a year starts or ends with Thursday, the year has 53 calendar weeks
       * 29th, 30th and 31st of December could belong to the first calendar week of the following year
       * 1st, 2nd and 3rd of January could belong to the last calendar week of the previous year
   In many places of the world (f.e. North-America, Australia) there is still the tradition of Jewry and Christianity where Sunday ist the first day of the week. In USA and other countries there are the following rules:
       * every Sunday starts an new calendar week
       * the first calendar week of the year starts on 1. January
   The result of these rules are following characteristics:
       * the first and the last calendar week of the year must not be complete, they can have less than 7 days
       * every year contains 53 calendar weeks
       * if the 31st of December is Sunday and the 1st of January of the same year was no Sunday, so this Sunday is the only day in the 54 calendar week. (This constellation happens very seldom, last in 2000, next in 2028).

Unfortunately Gregory Deatz had not read the rules exactly, so some functions are not standard of USA neather ISO (counts with week starting with Sunday). This is for functions F_YEAROFYEAR, F_WEEKOFYEAR and F_WOY. Please do not use this functions. Functions F_YEAR and F_WEEK are USA standard, functions F_KALENDERWOCHE and F_WOYISO are standard ISO.