Lesson

MySQL SUBSTR() Function

10 min reading
Free Course

MySQL SUBSTR() Function

The SUBSTR() function extracts a substring from a string beginning at a 1-based position. In MySQL, SUBSTR() is an exact synonym for SUBSTRING(), supporting both comma arguments and standard SQL FROM ... FOR ... syntax.

Syntax

SUBSTR(str, pos)
SUBSTR(str, pos, len)
SUBSTR(str FROM pos [FOR len])
  • str: Source string or column.
  • pos: 1-based start index (1 is the first character; negative numbers count backward from the end).
  • len: Optional maximum number of characters to extract.
  • Return value: Extracted substring, '' when pos is 0 or len is zero or negative, or NULL on NULL input.

SQLite and Oracle also support SUBSTR(), making it a popular portable choice across multi-database codebases. In MySQL, you can also omit len to capture everything from pos through the end of the string.

Practical Example

Employee badges in demo_employees follow the format 'DEPT-YEAR-ID' such as 'ENG-2024-081'. You can slice out the hire year and employee suffix with SUBSTR().

SELECT
    employee_id,
    badge_code,
    SUBSTR(badge_code, 5, 4) AS hire_year,
    SUBSTR(badge_code, -3) AS badge_seq
FROM demo_employees
ORDER BY employee_id;
employee_id badge_code hire_year badge_seq
1 ENG-2024-081 2024 081
2 OPS-2025-114 2025 114
3 HR-2023-009 2023 009

Look closely at row 3 in real datasets: if a department prefix has only two letters ('HR-2023-009'), fixed position 5 grabs '023-' instead of '2023'. Fixed offsets only stay accurate when segment widths remain uniform across every row.

Common Mistakes & NULL Behavior

  • Starting at index 0: Unlike Python or JavaScript, SQL string indexing starts at 1. Calling SUBSTR('ABC', 0, 2) returns an empty string '', not 'AB'.
  • Negative or zero len: Passing a zero or negative len returns ''; it never reads backward.
  • Variable prefix widths: When segment lengths vary before a delimiter like '-', switch to SUBSTRING_INDEX() instead of hardcoded SUBSTR() offsets.
  • NULL propagation: Any NULL argument in str, pos, or len yields NULL.

Frequently Asked Questions

Is there any difference between SUBSTR() and SUBSTRING()?

No. Both names point to the exact same implementation in MySQL and accept identical signatures, including FROM ... FOR ... syntax.

What happens if pos is negative and larger than the string length?

When ABS(pos) exceeds CHAR_LENGTH(str), SUBSTR() returns an empty string ''.

Does SUBSTR() handle UTF-8 accented characters safely?

Yes. Offsets count logical characters rather than raw bytes, so SUBSTR('café', 4, 1) cleanly returns 'é'.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum