SUBSTRING() in MySQL — How It Works
What the MySQL SUBSTRING() function does, with an example.
Published October 18, 2026
SUBSTRING(str, pos, len) returns part of a string starting at position pos (1-based) for len characters.
How it works
Negative positions count from the end. SUBSTR and MID are synonyms.
Try it in MySQL
SELECT SUBSTRING('abcdef', 2, 3)
bcd
SELECT SUBSTRING('abcdef', -2)
ef
SELECT SUBSTRING('abcdef', 3)
cdef
Practical examples
Example 1
SELECT SUBSTRING('abcdef', 2, 3);
-- bcdExample 2
SELECT SUBSTRING('abcdef', -2);
-- efExample 3
SELECT SUBSTRING('abcdef', 3);
-- cdefUse it in a real query
SELECT id, SUBSTRING('abcdef', 2, 3) AS result
FROM your_table
LIMIT 10;Common mistakes
- Using 0-based positions (MySQL is 1-based).
- Forgetting negative positions are valid.
- Using SUBSTRING in WHERE on indexed columns, disabling the index.
Key takeaways
- SUBSTRING() in one line: SUBSTRING(str, pos, len) returns part of a string starting at position pos (1-based) for len characters.
- Try queries in a test database first.
- Check NULL handling and data types.
FAQ
How do I get the first N characters?
Use LEFT(str, N).
Does SUBSTRING() work in other databases?
Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.