SQL SUBSTR
Take part of a text value.
Syntax
SUBSTR(text, start, length)
Positions start at 1. Leave out the length to go to the end.
Example
Query (on the shop database used in the SQL course)
SELECT order_date, SUBSTR(order_date, 1, 4) AS year, SUBSTR(order_date, 6, 2) AS month FROM orders LIMIT 4;
Result
| order_date | year | month |
|---|---|---|
| 2025-06-10 | 2025 | 06 |
| 2025-05-17 | 2025 | 05 |
| 2025-03-25 | 2025 | 03 |
| 2025-10-26 | 2025 | 10 |
4 rows
Good to know. PostgreSQL also has SUBSTRING(text FROM start FOR length).
Related
Learn it step by step in the free lesson Functions: Text, Numbers, Dates & CASE, with exercises that check your queries.