What Is SUBSTRING() in SQL?

SUBSTRING() extracts part of a text value in SQL. Supply a source string, a starting position (1-based), and an optional length. The function returns the requested slice as a new string, letting you isolate codes, dates, or any token inside a column without modifying the original data.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
SUBSTRING() returns a chosen slice of a text value. Pass the column or string, a 1-based start position, and an optional length to get back only the characters you need.

What Is the SQL SUBSTRING() Function?

SUBSTRING() extracts a specified slice of text from a string column or literal, returning only the characters you request.

Why Would You Use SUBSTRING()?

Use SUBSTRING() to isolate IDs, date parts, file extensions, or any token inside a longer value without updating the stored data.

What Is the Basic Syntax of SUBSTRING()?

The portable form is SUBSTRING(source FROM start FOR length); most engines also accept SUBSTRING(source, start, length).

How Does Positioning Work in SUBSTRING()?

Start positions are 1-based. A start of 1 returns the first character; negative starts count from the end in databases that allow it.

Can I Omit the Length Argument?

Yes. When length is omitted, SUBSTRING() returns every character from the start position to the end of the string.

How Do I Extract a Fixed-Length Code?

Pass the column, start, and length. Example: SUBSTRING(order_id FROM 1 FOR 3) returns the first three characters of every order_id.

How Do I Get the Last N Characters?

Combine LENGTH() with SUBSTRING(): SUBSTRING(sku FROM LENGTH(sku)-3) returns the last four characters.

How Can I Filter Rows With SUBSTRING() and LIKE?

Wrap SUBSTRING() inside WHERE and compare with LIKE. Example: WHERE SUBSTRING(email FROM 1 FOR 3) = 'dev'.

What’s the Performance Impact of SUBSTRING()?

SUBSTRING() is CPU-only and blocks index use on the operated column, so avoid it in WHERE for large tables or create computed indexes.

How Can I Chain SUBSTRING() With Other String Functions?

Nest SUBSTRING() inside CONCAT(), REPLACE(), or TRIM() to clean and reassemble strings in a single query step.

Is SUBSTRING() the Same Across Databases?

Core behavior is ANSI, but argument order, negative indexes, and alias names (e.g., SUBSTR) vary. Check your engine’s docs.

What Are Best Practices for SUBSTRING()?

Validate string length before slicing, document 1-based positions in comments, and create indexed computed columns for heavy filters.

Practical Examples

See code samples below for real-world patterns.

Common Mistakes to Avoid

Review the pitfalls section for off-by-one errors, NULL handling, and index misuse.

What Are the Key Takeaways?

SUBSTRING() is a reliable, portable way to extract text slices. Know the 1-based index, test edge cases, and optimize when used in filters.

Frequently Asked Questions (FAQs)

Does SUBSTRING() work on numbers?

Implicit casts let SUBSTRING() operate on numeric columns, but casting to TEXT first is clearer and avoids engine-specific behavior.

What happens if length exceeds the remaining characters?

SUBSTRING() stops at the string end without error, returning the available characters.

Is SUBSTR the same as SUBSTRING()?

Yes in most engines; SUBSTR is an alias. Always verify argument order and zero-based quirks.

How do I remove the first character quickly?

Use SUBSTRING(col FROM 2). Omitting length returns the rest of the string.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Truvideo Logo