string

SUBSTRING

Extracts a substring by 1-based start position and optional length. All three dialects support the comma-argument form; Postgres also accepts the SQL-standard FROM/FOR clauses and POSIX regex extraction.

SUBSTRING(string FROM start FOR length)  |  SUBSTRING(string, start, length)

Parameters / Modifiers

Parameter Purpose
FROM n Start position (1-based)
FOR len Number of characters to return
SUBSTRING(str FROM pattern) Postgres: regex extraction
SUBSTR Alias supported in SQLite/MySQL

Examples

SELECT SUBSTRING('hello world', 1, 5);

Returns 'hello'

SELECT SUBSTRING('hello world' FROM 7);

Postgres/SQL-standard: returns 'world' (from position 7 to end)

SELECT SUBSTRING(email, POSITION('@' IN email) + 1) FROM users;

Extract domain portion of each email

SELECT SUBSTRING('abc123' FROM '[0-9]+');

Postgres regex extraction — returns '123'

Dialect notes / Gotcha

Positions are 1-based, not 0-based. A negative start position is undefined in MySQL/Postgres; use RIGHT() or arithmetic with LENGTH() instead.

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet