title: Function Getting the right week number of year
body: <p>I want to create a function to get the right week number of year.
  I already posted <a href="https://stackoverflow.com/questions/14815929/getting-the-right-week-number-of-year/">here</a> to find a 'native' solution, but apparently there is not.</p>
  
  <p>I tryed to create funcrtion based on this <a href="https://stackoverflow.com/questions/10656996/week-of-the-year-for-weeks-starting-with-saturday?lq=1">mysql example</a></p>
  
  <p>Here is the code translated to postgresql:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION week_num_year(_date date)    
  RETURNS integer AS
  $BODY$declare 
  _year integer;
  begin
  
  
  select date_part('year',_date) into _year; 
  return ceil((to_char(_date,'DDD')::integer+(to_char(('01-01-'||_year)::date,'D')::integer%7-7))/7);           
  
  
  end;$BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;
  </code></pre>
  
  <p>But it gives wrong result, can someone help me ?</p>
  
  <p>My config: PostgreSQL 9.2</p>