DATE_FORMAT in SQL

Level 393 keystrokes

DATE_FORMAT converts date values to a specified string format.

SQL code

-- Return each user's name and the creation date as text
1SELECT
2 name,
-- Format created_at as 'YYYY-MM-DD' and drop the time of day
-- %Y = 4-digit year, %m = 2-digit month, %d = 2-digit day
3 DATE_FORMAT(created_at, '%Y-%m-%d') AS date
4FROM
5 users
-- Sort by the original datetime, newest first (DESC)
6ORDER BY
7 created_at DESC;

Output

+-------+------------+
| name  | date       |
+-------+------------+
| Carol | 2024-03-15 |
| Bob   | 2024-02-20 |
| Alice | 2024-01-10 |
+-------+------------+
3 rows in set
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • _× 3
  • %× 3
  • -× 2
  • ,× 2
  • '× 2
  • ;× 1
  • (× 1
  • )× 1

More SQL examples at level 3

← SQL typing practice