Date Functions in SQL

Level 3100 keystrokes

DATE_FORMAT converts a date to a specific string format.

SQL code

-- Return each user's name, the original timestamp and a formatted date
1SELECT
2 name,
3 created_at,
-- DATE_FORMAT turns the timestamp into text such as 2024-01-15
-- %Y = 4-digit year, %m = 2-digit month, %d = 2-digit day; AS date names the column
4 DATE_FORMAT(created_at, '%Y-%m-%d') AS date
-- Read the rows from the users table
5FROM
6 users
-- Sort by the original timestamp, oldest first (ASC is the default)
7ORDER BY
8 created_at;

Output

+-------+---------------------+------------+
| name  | created_at          | date       |
+-------+---------------------+------------+
| Alice | 2024-01-15 10:30:00 | 2024-01-15 |
| Bob   | 2024-02-20 14:45:00 | 2024-02-20 |
+-------+---------------------+------------+
2 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.

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

More SQL examples at level 3

← SQL typing practice