MySQL SUBSTRING Function: Syntax, Examples, and Tips

Formulas & functions · Updated November 23, 2026

The MySQL SUBSTRING() function extracts a portion of a string, returning a substring starting at a specified position and of a specified length. For example, SUBSTRING('Hello World', 7, 5) returns 'World'. This article explains the syntax, variants, common use cases, and pitfalls, with practical examples you can run immediately.

What Is SUBSTRING() in MySQL?

MySQL provides SUBSTRING() (and its synonyms SUBSTR() and MID()) to return a part of a string. The function is case-insensitive regarding its name, and it works with string literals, column values, and expressions. The basic syntax is:

SUBSTRING(str, pos)          -- from position pos to the end
SUBSTRING(str, pos, len)     -- from position pos, length len
SUBSTRING(str FROM pos)      -- equivalent to the first form
SUBSTRING(str FROM pos FOR len) -- equivalent to the second form

Positions are 1-based: position 1 is the first character. If pos is negative, the extraction starts from the end of the string. For example, SUBSTRING('abcdef', -3) returns 'def'.

How SUBSTRING() Works: Position and Length

The function reads the string left to right (or right to left for negative positions) and returns the requested slice. The len argument is optional; if omitted, the rest of the string is returned. If len is negative, MySQL returns an empty string (since version 8.0; older versions returned NULL). The function does not modify the original string; it returns a new string.

Example with a table column:

SELECT first_name, SUBSTRING(first_name, 1, 3) AS short_name
FROM employees
LIMIT 3;

If first_name is 'Jennifer', short_name becomes 'Jen'.

Using SUBSTRING() with FROM and FOR Syntax

The SQL-standard syntax uses FROM and FOR keywords, which can be more readable in complex queries. Both forms are equivalent:

SELECT SUBSTRING('MySQL is powerful' FROM 7 FOR 2) AS result;  -- returns 'is'

This syntax is particularly useful when the position or length is a column reference. For instance, to extract a variable-length code from a product code:

SELECT product_code, SUBSTRING(product_code FROM 3 FOR length_code) AS extracted
FROM products;

SUBSTRING() vs. LEFT() and RIGHT()

MySQL also has LEFT() and RIGHT() functions that extract from the beginning or end of a string. SUBSTRING() is more flexible because it can start anywhere. Use LEFT(str, n) for the first n characters, RIGHT(str, n) for the last n characters, and SUBSTRING() for any interior slice. For example, to get the first 3 characters of a name, LEFT(name, 3) is simpler than SUBSTRING(name, 1, 3). However, if you need the last 3 characters, RIGHT(name, 3) is clearer, though SUBSTRING(name, -3) also works.

Common Use Cases and Real-World Examples

Here are typical scenarios where SUBSTRING() is used:

  • Extracting a year from a date stored as a string: SUBSTRING('2023-04-15', 1, 4) returns '2023'.
  • Parsing a domain from an email: SUBSTRING(email, LOCATE('@', email) + 1) gives the part after the @.
  • Masking sensitive data: CONCAT(SUBSTRING(phone, 1, 3), '****', SUBSTRING(phone, 8)) shows only the first three and last two digits.
  • Sorting by a substring: ORDER BY SUBSTRING(code, 2, 3) sorts by the middle three characters.

Example: Extract the area code from a phone number column phone (format: ‘123-456-7890’):

SELECT phone, SUBSTRING(phone, 1, 3) AS area_code
FROM contacts;

Performance Considerations and Indexes

Using SUBSTRING() in a WHERE clause can prevent index usage because the function must be evaluated for every row. For example, WHERE SUBSTRING(last_name, 1, 1) = 'A' will force a full table scan. If you need to filter by a prefix, use LIKE 'A%' or a generated column with an index. For extracting a substring and then comparing, consider storing the extracted value in a separate column if the query runs frequently. In SELECT lists, SUBSTRING() has minimal overhead, but in large aggregations it can add CPU cost.

Handling NULLs and Edge Cases

If any argument is NULL, SUBSTRING() returns NULL. If pos is 0, MySQL returns an empty string. If pos is greater than the string length, an empty string is returned. If len is negative, behavior depends on the MySQL version: before 8.0, it returned NULL; from 8.0 onward, it returns an empty string. Always test your version. For example:

SELECT SUBSTRING('abc', 0) AS r1,      -- ''
       SUBSTRING('abc', 5) AS r2,      -- ''
       SUBSTRING('abc', -1) AS r3,     -- 'c'
       SUBSTRING('abc', 2, -1) AS r4;  -- '' (in MySQL 8.0)

Frequently Asked Questions

What is the difference between SUBSTRING() and SUBSTR()?

There is no difference; SUBSTR() is a synonym for SUBSTRING() in MySQL. You can use either name, and both accept the same arguments. MID() is also a synonym, though less commonly used.

Can I use SUBSTRING() with a negative length?

In MySQL 8.0 and later, a negative length returns an empty string. In earlier versions (e.g., 5.7), it returns NULL. It does not count backwards from the end; it simply yields no characters. To extract from the end, use a negative starting position instead.

How do I extract the last N characters from a string?

You can use RIGHT(str, N) or SUBSTRING(str, -N). For example, SUBSTRING('abcdef', -2) returns 'ef'. Both work, but RIGHT() is more readable.

Does SUBSTRING() work with binary strings?

Yes, SUBSTRING() works with binary strings (BINARY, VARBINARY, BLOB). It operates on bytes, not characters. For multi-byte character sets like UTF-8, it operates on characters, not bytes, so it is safe for text. If you need byte-level slicing, cast to BINARY first.

Is there a way to split a string into multiple parts?

MySQL has no built-in SPLIT_PART function, but you can combine SUBSTRING() with LOCATE() and REPLACE() to parse delimited strings. For example, to get the second comma-separated value:

SELECT SUBSTRING(
  'apple,banana,cherry',
  LOCATE(',', 'apple,banana,cherry') + 1,
  LOCATE(',', 'apple,banana,cherry', LOCATE(',', 'apple,banana,cherry') + 1) - LOCATE(',', 'apple,banana,cherry') - 1
) AS second_value;  -- returns 'banana'

Alternatively, use SUBSTRING_INDEX() for simpler cases.

How is SUBSTRING() different from SUBSTRING_INDEX()?

SUBSTRING() extracts by position and length. SUBSTRING_INDEX(str, delim, count) returns the part of a string before or after a delimiter. For example, SUBSTRING_INDEX('a,b,c', ',', 2) returns 'a,b'. They serve different purposes; use SUBSTRING_INDEX() for delimiter-based splitting.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.