SQL Standard

Extract a Substring After a Character in SQL

String extraction refers to the process of extracting a specific portion or a substring from a given string or text. This is a common task in various fields of development and database manipulation. Using the string extraction techniques, we can fetch and perform the quick pattern matching using native SQL methods.

In this tutorial, we will learn how to extract a substring after a specific character using the SQL methods and techniques.

This tutorial uses generic SQL, and some methods and functions that are discussed may be missing from your SQL engine. Consult your engine manual for the method equivalent.

Extract a Substring After a Character in SQL

To extract a substring after a specific character in SQL, we can use the SUBSTRING() function in combination with the CHARINDEX() function.

The substring() function allows us to extract a specific substring from a larger string by providing three main arguments:

    1. The first argument is the input string which refers to the string that we wish to extract.
    2. The second argument is the starting position of the substring counting from 1.
    3. Finally, we provide the length of the substring that we wish to extract.

Next is the charindex() method. This method allows us to find the position of a given character within a string. The function takes two arguments:

    1. The character or substring that we wish to search
    2. The input string

The method returns the character’s position or substring as an integer value. We can then pass this integer to the starting position parameter of the substring() function.

We can express the syntax of the these methods as follows:

SELECT SUBSTRING(input_string, CHARINDEX(search_character, input_string) + 1, LEN(input_string))

Let us break down the provided syntax:

    1. input_string – This parameter specifies the string that we wish to extract fom the substring.
    2. search_character – It defines the character after which we wish to extract the substring.
    3. CHARINDEX(search_character, input_string) + 1 – This operation returns the position of the first occurrence of search_character in the input string, plus one. We want to extract the substring after the character, not including the character itself.
    4. LEN(input_string) – This function returns the length of the input string which is used as the length argument for the substring() function which ensures that we extract the entire substring after the search charater.

Suppose we have a table called “country_information”. Consider the following query:

SELECT SUBSTRING(country, CHARINDEX('U', country) + 1, LEN(country))
FROM country_information;

The previous query extracts a substring from the country column for each row in the country_information table, starting from the character after the first occurrence of “U” in the country column.


We learned how to combine the substring() and the charindex() functions to extract a substring after a character in SQL. As mentioned, the methods that are discussed in this tutorial may not be available in all SQL engines. Consider the documentation for your engine to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list