Date formatting
- SQL course
- Beginner
- Lesson 57/115
- 4 min
Display dates in different formats.
DATE_FORMAT
DATE_FORMAT (MySQL) or TO_CHAR (PostgreSQL) formats a date as text in a given pattern.
SELECT DATE_FORMAT(order_date, '%Y-%m-%d') AS metin FROM orders;This query converts the date to YYYY-MM-DD text.
DATE_FORMAT(order_date, '%Y-%m-%d') AS metin is selected from orders.
DATE_FORMAT — details
Format tokens differ by database: MySQL uses %Y, %m, %d; PostgreSQL uses YYYY, MM, DD.
Practice DATE_FORMAT
Format order_date as %Y-%m-%d text from orders.
Starter code
-- format
SELECT
FROM ordersSample solution
SELECT DATE_FORMAT(order_date, '%Y-%m-%d') AS metin
FROM ordersExpected output
metin 2026-01-15 2026-02-01
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Format
SELECT ____(order_date, '%Y-%m-%d') FROM orders;
SELECT DATE_FORMAT(order_date, '%Y-%m-%d') FROM orders;
Explanation: DATE_FORMAT is the right answer.
2 Date formatting: match the terms
| Term | Meaning |
|---|---|
DATE_FORMAT |
format |
EXTRACT |
extract |
DATEDIFF |
diff |
CURRENT_DATE |
today |
3 What does DATE_FORMAT do?
- Compares dates
- Adds to a date
- Deletes a date
- Converts a date to text Correct answer
Explanation: DATE_FORMAT formats a date as text.