# Sorting by Day of Week

6.10.1 Problem

You want to sort in day-of-week order.

6.10.2 Solution

Use DAYOFWEEK( ) to convert a date column to its numeric day of week value.

6.10.3 Discussion

Day-of-week sorting is similar to calendar day sorting, except that you use different functions to get at the relevant ordering values.

You can get the day of the week using DAYNAME( ), but that produces strings that sort lexically rather than in day-of-week order (Sunday, Monday, Tuesday, etc.). Here the technique of displaying one value but sorting by another is useful (Recipe 6.5). Display day names using DAYNAME( ), but sort in day-of-week order using DAYOFWEEK( ), which returns numeric values from 1 to 7 for Sunday through Saturday:

```mysql> SELECT DAYNAME(date) AS day, date, description
-> FROM event
-> ORDER BY DAYOFWEEK(date);
+----------+------------+-------------------------------------+
| day | date | description |
+----------+------------+-------------------------------------+
| Sunday | 1776-07-14 | Bastille Day |
| Sunday | 1809-02-12 | Abraham Lincoln's birthday |
| Monday | 1215-06-15 | Signing of the Magna Carta |
| Tuesday | 1944-06-06 | D-Day at Normandy Beaches |
| Thursday | 1989-11-09 | Opening of the Berlin Wall |
| Friday | 1957-10-04 | Sputnik launch date |
| Friday | 1958-01-31 | Explorer 1 launch date |
| Friday | 1732-02-22 | George Washington's birthday |
| Saturday | 1789-07-04 | US Independence Day |
| Saturday | 1919-06-28 | Signing of the Treaty of Versailles |
+----------+------------+-------------------------------------+```

If you want to sort in day-of-week order, but treat Monday as the first day of the week and Sunday as the last, you can use a the MOD( ) function to map Monday to 0, Tuesday to 1, ..., Sunday to 6:

```mysql> SELECT DAYNAME(date), date, description
-> FROM event
-> ORDER BY MOD(DAYOFWEEK(date) + 5, 7);
+---------------+------------+-------------------------------------+
| DAYNAME(date) | date | description |
+---------------+------------+-------------------------------------+
| Monday | 1215-06-15 | Signing of the Magna Carta |
| Tuesday | 1944-06-06 | D-Day at Normandy Beaches |
| Thursday | 1989-11-09 | Opening of the Berlin Wall |
| Friday | 1957-10-04 | Sputnik launch date |
| Friday | 1958-01-31 | Explorer 1 launch date |
| Friday | 1732-02-22 | George Washington's birthday |
| Saturday | 1789-07-04 | US Independence Day |
| Saturday | 1919-06-28 | Signing of the Treaty of Versailles |
| Sunday | 1776-07-14 | Bastille Day |
| Sunday | 1809-02-12 | Abraham Lincoln's birthday |
+---------------+------------+-------------------------------------+```

The following table shows the DAYOFWEEK( ) expressions to use for putting any day of the week first in the sort order:

Day to list first

DAYOFWEEK( ) expression

Sunday

DAYOFWEEK(date)

Monday

MOD(DAYOFWEEK(date) + 5, 7)

Tuesday

MOD(DAYOFWEEK(date) + 4, 7)

Wednesday

MOD(DAYOFWEEK(date) + 3, 7)

Thursday

MOD(DAYOFWEEK(date) + 2, 7)

Friday

MOD(DAYOFWEEK(date) + 1, 7)

Saturday

MOD(DAYOFWEEK(date) + 0, 7)

Another function that you can use for day-of-week sorting is WEEKDAY( ), although it returns a different set of values (0 for Monday through 6 for Sunday).

MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2005
Pages: 412
Authors: Paul DuBois

Similar book on Amazon