MySQL Forums
Forum List  »  Docs

Re: YearWeek() addition to documentation
Posted by: M. G.
Date: October 02, 2009 03:31PM

I'll try (monospaced text is my addition)

YEARWEEK(date), YEARWEEK(date,mode)
Returns year and week for a date. The mode argument works exactly like the mode argument to WEEK(). The year in the result may be different from the year in the date argument for the first and the last week of the year.
When used as an aggregate function YEARWEEK() results will not split across year boundaries:

mysql> CREATE TABLE Dated_items (Purchase_date DATE, Price DECIMAL(6,2));
mysql> INSERT INTO Dated_items Values
       ('2007-01-01',10.0),('2007-01-2', 20.0),
       ('2007-01-3', 15.0),('2007-01-4', 25.0),
       ('2007-01-5', 30.0),('2007-01-8', 50.0);
mysql> SELECT YearWeek(Purchase_Date) AS Week, SUM(Price) as Total
    ->        FROM dated_items GROUP BY Week;
+--------+--------+
| Week   | Total  |
+--------+--------+
| 200653 | 100.00 |
| 200701 |  50.00 |
+--------+--------+
2 rows in set (0.00 sec)

mysql> SELECT YEARWEEK('1987-01-01');
-> 198653

Note that the week number is different from what the WEEK() function would return (0) for optional arguments 0 or 1, as WEEK() then returns the week in the context of the given year.

Regards,
Meir



Edited 1 time(s). Last edit at 10/03/2009 01:09AM by Jon Stephens.

Options: ReplyQuote


Subject
Views
Written By
Posted
8188
September 03, 2009 05:58AM
Re: YearWeek() addition to documentation
7664
October 02, 2009 03:31PM
3466
October 03, 2009 07:26AM
3420
October 30, 2009 06:36AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.