Home / SQL / Reference / Text functions

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_dateyearmonth
2025-06-10202506
2025-05-17202505
2025-03-25202503
2025-10-26202510

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.