Sorting in order of days of birth
Such a sorting may be useful for getting the order of celebration of employees’ DOB.
The feature of this sorting is in ordering dates firstly by month, then by day of month without taking into account the year of birth at all.
Let’s consider the Battles table as an example, namely date column. It is evident that ordering by date does not required result due to the year (for example, October 20 follows after November 15):
select date
from Battles
order by date;
[[ column ]] |
---|
NULL [[ value ]] |
date |
---|
1941-05-25 00:00:00.000 |
1942-11-15 00:00:00.000 |
1943-12-26 00:00:00.000 |
1944-10-25 00:00:00.000 |
1962-10-20 00:00:00.000 |
1962-10-25 00:00:00.000 |
To solve the problem, two methods can be suggested (in SQL Server dialect).
1. Use of CONVERT function
In so doing, we transform datetime value to the string representation in the format “mm-dd”
select convert(CHAR(5), date, 110) "mm-dd"
from Battles;
[[ column ]] |
---|
NULL [[ value ]] |
and the latter is used in sorting
select date
from Battles
order by convert(CHAR(5),date,110);
[[ column ]] |
---|
NULL [[ value ]] |
date |
---|
1941-05-25 00:00:00.000 |
1962-10-20 00:00:00.000 |
1962-10-25 00:00:00.000 |
1944-10-25 00:00:00.000 |
1942-11-15 00:00:00.000 |
1943-12-26 00:00:00.000 |
2. Use of MONTH and DAY functions
Here we use built-in functions which return date components - month (MONTH) and day (DAY) respectively. Let’s do sorting on these components:
select date
from Battles
order by MONTH(date), DAY(date);
[[ column ]] |
---|
NULL [[ value ]] |
As regards query performance, you can choose any method because optimizer produces identical execution plans for these.
Finally let’s give the last query in a more presentable form having included in it additionally a “hero of the festivities”:
select DAY(date) BD_day, DATENAME(mm, date) BD_month, name
from Battles
order by MONTH(date), DAY(date);
[[ column ]] |
---|
NULL [[ value ]] |
The CONVERT function is specific to SQL Server, meaning it is most likely not available in other DBMSs.
To solve our problem of sorting by birthdays in other DBMS, we will follow the first way, i.e. use a suitable function that converts the date to the text format “MM-DD” (month-day).
MySQL
In MySQL, such a function is DATE_FORMAT:
SELECT date,DATE_FORMAT(date,'%m-%d') "m-d"
FROM Battles;
[[ column ]] |
---|
NULL [[ value ]] |
Here is result
date | m-d |
---|---|
1962-10-20 00:00:00 | 10-20 |
1962-10-25 00:00:00 | 10-25 |
1942-11-15 00:00:00 | 11-15 |
1941-05-25 00:00:00 | 05-25 |
1943-12-26 00:00:00 | 12-26 |
1944-10-25 00:00:00 | 10-25 |
Accordingly, a query that sorts by “birthdays” would look like this:
SELECT date
FROM Battles
ORDER BY DATE_FORMAT(date,'%m-%d');
[[ column ]] |
---|
NULL [[ value ]] |
PostgreSQL & Oracle
PostgreSQL and Oracle have a TO_CHAR function that specifies the format of the text representation of a date. Here is an example query (PostgreSQL) that returns a result similar to the one shown above for MySQL.
SELECT date, TO_CHAR(date,'mm-dd') "m-d"
FROM Battle;
[[ column ]] |
---|
NULL [[ value ]] |
The only difference for Oracle is not in the syntax of the function itself, but that the keyword date
must be enclosed in double quotes to indicate that it is a column name, not a data type.
Here is the example of what sorting by birthdays looks like for Oracle:
SELECT "date"
FROM Battles
ORDER BY TO_CHAR("date",'mm-dd');
[[ column ]] |
---|
NULL [[ value ]] |
date |
1941-05-25 00:00:00 |
1962-10-20 00:00:00 |
1962-10-25 00:00:00 |
1944-10-25 00:00:00 |
1942-11-15 00:00:00 |
1943-12-26 00:00:00 |