Tuesday, December 15, 2009

Yearweek function in SQL

A neat function in SQL which gives you the year and the week of the date it is presented with:
SELECT YEARWEEK('2009-12-23')
returns 200951, that is the 51 week of 2009.

SELECT YEARWEEK('2010-01-02')
returns 200952, the last week in 2009. As one can see the year here is different from the input value, this is because January 2nd 2010 is a Saturday and therefore belongs to the last week in 2009. First week of 2010 begins on Sunday 3rd or Monday 4th depending on how it is defined in your country.

Solution source: http://www.java2s.com/Tutorial/MySQL/0280__Date-Time-Functions/YEARWEEKdateYEARWEEKdatemodereturnsyearandweekforadate.htm

No comments:

Post a Comment