

ISO WEEK NUMBER ISO
date - The date for which to determine the ISO week number.Let p_week = mod(p_days - mod(p_days, 10), 100) / 10 Ĭreate or replace procedure "informix".date2week(_date date) returning char(7) - "yyyy.ww"define p_day smallint define p_month smallint define p_year smallint define p_firstweekday smallint define p_days smallint define p_week char(7) if (_date is null) then let _date = today end if let p_week = " " let p_day = day(_date) let p_month = month(_date) let p_year = year(_date) let p_firstweekday = weekday(mdy(1, 1, p_year)) let p_days = (_date - mdy(1, 1, p_year) + p_firstweekday - 1) / 7 if (p_firstweekday > 4) then if (p_days = 0) then let p_week = date2week(mdy(12, 31, p_year - 1)) let p_days = p_week let p_year = p_year - 1 else if ((p_firstweekday = 7) and ((mod(p_year, 4) = 0) and((mod(p_year, 100) > 0) or (mod(p_year, 400) = 0))) and (p_month = 12)and (p_day = 31)) then let p_days = 1 let p_year = p_year + 1 end if end if else if (_date >= mdy(12, 29, p_year)) then let p_firstweekday = weekday(mdy(12, p_day, p_year)) if (p_day = 31) then if ((p_firstweekday = 1) or (p_firstweekday = 2) or(p_firstweekday = 3)) then let p_days = 0 let p_year = p_year + 1 end if elif (p_day = 30) then if ((p_firstweekday = 1) or (p_firstweekday = 2)) then let p_days = 0 let p_year = p_year + 1 end if elif (p_day = 29) then if (p_firstweekday = 1) then let p_days = 0 let p_year = p_year + 1 end if end if end if let p_days = p_days + 1 end if let p_week = p_year let p_week = "." let p_week = mod(p_days - mod(p_days, 10), 100) / 10 let p_week = mod(p_days, 10) return (p_week) end procedure Returns the number of the ISO (International Organization for Standardization) week of the year where the provided date falls. Then if ((p_firstweekday = 1) or (p_firstweekday = 2)) Then if ((p_firstweekday = 1) or (p_firstweekday = 2) or Then let p_firstweekday = weekday(mdy(12, p_day, p_year)) Then let p_week = date2week(mdy(12, 31, p_year - 1)) Įlse if ((p_firstweekday = 7) and ((mod(p_year, 4) = 0) and Let p_days = (_date - mdy(1, 1, p_year) + p_firstweekday - 1) / 7 Let p_firstweekday = weekday(mdy(1, 1, p_year)) Can't find the original posting now, so I don't know who the original author is, perhaps Jonathan Leffler?Ĭreate or replace procedure "informix".date2week(_date date) returning char(7) - "yyyy.ww" I think this was posted to the old newsgroup many, many years ago. SELECT ymd, (d3 / 7 + 1)::int iwFROM(SELECT dt ,TO_CHAR(dt, '%Y%m%d')::char(10) ymd ,(((TRUNC(dt - 1 units day, 'day') - NEXT_DAY(TRUNC(TRUNC(dt - 1 units day, 'day') - 1 units day, 'year'), 'SUN'))::interval day(3) to day)::varchar(12)::int ) d3 FROM (SELECT TO_DATE(yy||'01', '%Y%m') + (LEVEL - 1) units day dt FROM (SELECT '2021' yy FROM dual) CONNECT BY LEVEL <= 366)) Mi_integer oni_isoweek(mi_datetime *p_dte, MI_FPARAM *fp)

ISO WEEK NUMBER CODE
Write a C UDR - it will be quicker, something like this - pseudo code 100% not tested

,(((TRUNC(dt - 1 units day, 'day') - NEXT_DAY(TRUNC(TRUNC(dt - 1 units day, 'day') - 1 units day, 'year'), 'SUN'))::interval day(3) to day)::varchar(12)::int ) d3 Please advise on what is wrong or what needs to be improved.

I wrote a messy query like the one below. I looked at the post below, but it didn't help much. I'm trying to convert to week number (1-53) for a specific date.
