postgresql - Query for computing yearweek -
i need @ query in such way in yearweek,in year there 52 weeks
2015 has 52 weeks 2016 yearweek should start 201601 first week of year 2016, not continuation of 2015's 53rd week.
2015 jan 1--> 201501(1st week of 2015) likewise 2016 jan 1 --> 201601(1st week of 2016) shouldnt 201553(53rd week of 2015)
try this
create or replace function fn_yearofweek (val date) returns text $$ select extract(year val)::text || cast(floor((extract(doy val) - 1) / 7) + 1 text); $$ language sql
usage:
select fn_yearofweek('2015-01-01') ,fn_yearofweek('2015-12-31') ,fn_yearofweek('2016-01-01');
result:
fn_yearofweek fn_yearofweek fn_yearofweek ------------- ---------------- ---------------- 20151 201553 20161
Comments
Post a Comment