SQLite 3 reference · String Functions
SQLite SUBSTR() Function
Return part of a string starting at a 1-based position, with an optional length. Negative starts count from the end.
SQLite 3
Syntax
substr(X, start, length) | substring(X, start, length)
Parameters
| Parameter | Description |
|---|---|
X |
The string (other values are converted to text). |
start |
The first character, counting from 1; negative counts back from the end. |
length optional |
How many characters to take; omit it to take the rest. |
Return value
Text (or a BLOB slice), or NULL.
Examples
SELECT substr('ReadyCamel', 1, 5) AS first5,
substr('ReadyCamel', 6) AS rest,
substr('ReadyCamel', -5, 3) AS from_end;
Output
first5 | rest | from_end Ready | Camel | Cam
SELECT order_date, substr(order_date, 1, 7) AS month
FROM orders
LIMIT 3;
Output
order_date | month 2026-01-05 | 2026-01 2026-01-12 | 2026-01 2026-02-02 | 2026-02
Common mistakes
- Positions start at 1, not 0.
substr(x, 0, 3)returns only the first two characters.
Version history
| Version | Change |
|---|---|
| SQLite 3.34.0 | substring() is accepted as another name for substr(). |
In other languages
SUBSTRING()· PostgreSQLLEFT()· PostgreSQL
Related
INSTR()· Return the 1-based position of the first occurrence of one string inside another, or 0 if it isn't found.REPLACE()· Replace every occurrence of one string with another inside a string. The search is case-sensitive.LENGTH()· Return the number of characters in a string, or the number of bytes in a BLOB. NULL gives NULL.- SQL String Functions · tutorial