SQL
SELECT statement
string manipulation
database query
SQL functions

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.

Practice system design

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.

sql
1SELECT
2    order_code,
3    SUBSTRING(order_code, CHARINDEX('-', order_code) + 1, LEN(order_code)) AS after_dash
4FROM orders;

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:

sql
1SELECT
2    order_code,
3    CASE
4        WHEN CHARINDEX('-', order_code) > 0
5        THEN SUBSTRING(order_code, CHARINDEX('-', order_code) + 1, LEN(order_code))
6        ELSE NULL
7    END AS after_dash
8FROM orders;

MySQL Example

In MySQL, SUBSTRING_INDEX is often the simplest option when you want the text after a delimiter.

sql
1SELECT
2    order_code,
3    SUBSTRING_INDEX(order_code, '-', -1) AS after_last_dash
4FROM orders;

That returns everything after the last hyphen. If you specifically want everything after the first hyphen, combine LOCATE and SUBSTRING:

sql
1SELECT
2    order_code,
3    CASE
4        WHEN LOCATE('-', order_code) > 0
5        THEN SUBSTRING(order_code, LOCATE('-', order_code) + 1)
6        ELSE NULL
7    END AS after_first_dash
8FROM orders;

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:

sql
1SELECT
2    order_code,
3    CASE
4        WHEN POSITION('-' IN order_code) > 0
5        THEN SUBSTRING(order_code FROM POSITION('-' IN order_code) + 1)
6        ELSE NULL
7    END AS after_dash
8FROM orders;

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:

sql
SELECT SUBSTRING_INDEX('a/b/c.txt', '/', -1) AS filename;

In PostgreSQL:

sql
SELECT REGEXP_REPLACE('a/b/c.txt', '^.*/', '') AS filename;

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 CHARINDEX plus SUBSTRING.
  • MySQL often uses SUBSTRING_INDEX, LOCATE, and SUBSTRING.
  • PostgreSQL commonly uses POSITION plus SUBSTRING.
  • Decide whether you need the text after the first delimiter or the last one, and handle missing delimiters explicitly.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.