If you want to use the correct week numbers:
select extract(week from '2012-01-01'::date);
This will result in result 52 , which is correct if you look at the calendar.
Now, if you really want to define the number of weeks as “Every 7 days, starting on the first day of the year,” that’s good, although it doesn’t match the week numbers that anyone uses and has some odd quirks:
select floor((extract(doy from '2011-01-01'::date)-1)/7)+1;
By the way, parsing date strings and breaking them with string functions is almost always very bad.
Craig Ringer
source share