SQL SUBSTRING function
Returns a slice of a string by position. Every engine counts from 1, but the name (SUBSTRING or SUBSTR), the optional length and the meaning of a negative start all vary.
- Catégorie
- Strings
- Returns
string- In this category
- 2 of 172
Syntaxe
SUBSTRING(string, start [, length])
SUBSTRING(string FROM start FOR length)
SUBSTR(string, start [, length])
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Oui | length is required before SQL Server 2025 |
| PostgreSQL | Oui | Supported under this name. |
| MySQL | Oui | Supported under this name. |
| SQLite | Oui | SUBSTR, and SUBSTRING from 3.34 |
| Oracle | Different name or syntax | SUBSTR only; there is no SUBSTRING |
Exemples
SQL Server
-- also PostgreSQL, MySQL and SQLite; Oracle spells it SUBSTR
SELECT SUBSTRING('2026-08-01', 1, 4) AS year; -- 2026
PostgreSQL
SELECT SUBSTRING('abcdef' FROM 2 FOR 3) AS mid; -- bcd
Oracle
SELECT SUBSTR('abcdef', -3) AS tail FROM dual; -- def
Bon à savoir
- Positions are 1-based. Start 0 is not an error but differs: SUBSTRING('abc', 0, 2) is 'a' on SQL Server, PostgreSQL and SQLite (position 0 eats one of the two characters), '' on MySQL, and 'ab' on Oracle, which treats 0 as 1.
- SQL Server required all three arguments until SQL Server 2025 made length optional. PostgreSQL, MySQL, SQLite and Oracle let you omit length to take the rest of the string.
- A negative start counts from the end on MySQL, SQLite and Oracle: SUBSTR('abcdef', -3) is 'def'. On SQL Server and PostgreSQL it is a position before the string, so SUBSTRING('abcdef', -2, 4) is just 'a'.
Questions about SUBSTRING / SUBSTR
Is SUBSTRING 0-based or 1-based?
1-based in every major engine. Position 0 does not raise an error, but the result differs by engine: one character short on SQL Server, PostgreSQL and SQLite, empty on MySQL, unchanged on Oracle.
Why does my SUBSTRING need three arguments on SQL Server?
Before SQL Server 2025, SUBSTRING required both start and length; pass LEN(s) as the length to take the rest. SQL Server 2025, PostgreSQL, MySQL, SQLite and Oracle all accept the two-argument form.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Column-prefix indexes in every SQL dialect Dialect comparison
- Find duplicate index candidates Snippet
- Extract leading digits from a string in SQL Snippet
- Extract trailing digits from a string in SQL Snippet
- Mask payment card numbers in SQL (show last 4) Snippet
- Natural sort strings with numbers in SQL Snippet
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.