SQL SELECT everything after a certain character
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Selecting everything after a certain character in SQL is a string-splitting problem. The general pattern is: find the delimiter position, then return the substring that starts one character later.
Core Sections
The Core Idea
Suppose a column contains values like ORD-2026-001, and you want everything after the first hyphen. You need two pieces:
- a function that finds the delimiter position
- a function that extracts the remainder of the string
The exact function names vary by database engine, but the approach is the same.
SQL Server Example
In SQL Server, use CHARINDEX with SUBSTRING.
CHARINDEX('-', order_code) returns the position of the first hyphen. SUBSTRING then starts one character after that position and returns the rest of the string.
If the delimiter might not exist, guard the expression:
MySQL Example
In MySQL, SUBSTRING_INDEX is often the simplest option when you want the text after a delimiter.
That returns everything after the last hyphen. If you specifically want everything after the first hyphen, combine LOCATE and SUBSTRING:
The distinction between first and last delimiter matters when the string contains more than one separator.
PostgreSQL Example
In PostgreSQL, POSITION and SUBSTRING work well:
PostgreSQL also supports regular expressions for more complex extractions, but the basic substring approach is clearer when the requirement is simple.
Everything After the Last Delimiter
Sometimes "after a certain character" really means "after the last occurrence." The function choice changes slightly.
In MySQL:
In PostgreSQL:
In SQL Server, reversing the string can help, though it is less elegant than MySQL's built-in shortcut.
Clean up the extracted result
In real data, delimiters are often followed by spaces. If the input looks like key: value, the raw substring may begin with a leading blank. Wrapping the result in TRIM(...) or the database-specific equivalent keeps the extracted text ready for display or comparison without another processing step later.
If you only need the parsed value for display, computing it in the SELECT list is fine. If you need to filter or join on it often, consider storing the parsed value separately or exposing it through a computed column.
Common Pitfalls
- Ignoring the case where the delimiter does not exist in the string.
- Confusing "after the first delimiter" with "after the last delimiter."
- Forgetting to trim leading spaces when the delimiter is followed by whitespace.
- Assuming the same string function names and semantics exist across every SQL engine.
- Applying string parsing inside large filtering queries without thinking about index impact.
Summary
- The general SQL pattern is "find delimiter position, then take the substring after it."
- SQL Server commonly uses
CHARINDEXplusSUBSTRING. - MySQL often uses
SUBSTRING_INDEX,LOCATE, andSUBSTRING. - PostgreSQL commonly uses
POSITIONplusSUBSTRING. - Decide whether you need the text after the first delimiter or the last one, and handle missing delimiters explicitly.
Related reading
- SQL select only rows with max value on a column
- SQL select only rows with max value on a column
- SQL SELECT WHERE field contains words
- SQL Server 2005 Replication
- SQL Server 2008 Replication avoiding reinitialization
- SQL Server 2014 - Missing option on Replication
- SQL Server Bi-Directional Transactional Replication - Is it a good use-case?
- SQL Server Msmerge_content

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.