0

Here is the proper ISO 8601 WeekOfYear function, Enjoy!

drop function if exists getISOWeekOfYear;
create function getISOWeekOfYear
(
ADate date
)
returns int
begin
  declare a int;
  declare b int;
  declare c int;
  declare s int;
  declare e int;
  declare f int;
  declare g int;
  declare d int;
  declare nn int;

  declare woy int;

  declare mt int;
  declare dy int;
  declare yr int;

  set mt = extract(month from ADate);
  set dy = extract(day from ADate);
  set yr = extract(year from ADate);

  if mt<3 then
    set a=yr-1; else set a=yr;
  end if;

  set b= (a/4) - (a/100) + (a/400);
  set c= ((a-1)/4) - ((a-1)/100) + ((a-1)/400);
  set s=b-c;

  if mt<3 then
    set e=0;
    set f=dy -1 + (31*(mt-1));
  else
    set e=s+1;
    set f=dy+58+s+(153*(mt-3)+2)/5;
  end if;

  set g = mod(a+b,7);
  set d = mod(f+g-e,7);
  set nn = f+3-d;

  if nn<0 then
    set woy=(53-((g-s)/5));
  elseif nn>(364+s) then
    set woy=1;
  else
    set woy=(nn/7)+1;
  end if;
  return woy;
end;
Category: 
SQL Functions & Procedures
Current Version: 
1.00
Supported Products: 
NexusDB V2
NexusDB V3
Home