How to retrieve a set of characters using SUBSTRING in SQL?

Last updated on Mar 09,2023 25.5K Views

How to retrieve a set of characters using SUBSTRING in SQL?

edureka.co

Structured Query Language aims to give users the ability to retrieve the data in a format they want. In this article on a substring in SQL, I will show you how to extract a set of characters from a string. The following topics will be covered in this article:

Let us get started!

What is SQL? 

SQL or Structured Query Language is developed by Donald D.Chamberlin and is used to manage, access and retrieve data in a database. It consists of various commands segregated into 4 categories (DDL, DML, DCL, and TCL). SUBSTRING is one such command in SQL, used to retrieve a set of characters from the specified string.

Next, in this article let us delve deeper into what is SUBSTRING in SQL and how to use it.

What is SUBSTRING in SQL?

SUBSTRING in SQL is a function used to retrieve characters from a string. With the help of this function, you can retrieve any number of substrings from a single string.

Syntax:

SUBSTRING(string, starting_value, length)

Here,

Refer the image below for the pictorial representation of SUBSTRING in SQL.

Note:  

Since you have understood the syntax and the rules to use the SUBSTRING in SQL, let us now discuss the various ways to use it.

SUBSTRING Examples:

For your better understanding, I have divided the examples into following sections:

    1. Use SUBSTRING on Literals
    2. Use SUBSTRING on Table with conditions
    3. USE SUBSTRING on Nested Queries

Let us look into each one of them.

Use SUBSTRING on Literals

When you use SUBSTRING in SQL for literals, it extracts a substring from the specified string with a length and the starting from the initial value mentioned by the user.

Example 1

Write a query to extract a substring from the string “Edureka”, starting from the 2nd character and must contain 4 characters.

 
SELECT SUBSTRING(‘Edureka’, 2, 4);

Output

dure

Example 2 

Write a query to extract a substring of 8 characters, starting from the 2nd character from the string “Edureka”. Here, if you observe, we need to extract a substring with the length greater than the maximum length of the expression.

SELECT SUBSTRING(‘Edureka’, 2, 8);

Output

dureka

Use SUBSTRING on Table with conditions

Consider the below table with the table name Customers.

CustID

CustName

CustEmail

1

Anuj

anuj@abc.com

2

Akash

akash@xyz.com

3

Mitali

mitali@pqr.com

4

Sonali

sonali@abc.com

5

Sanjay

sanjay@xyz.com

If you wish to know how to create a a table and insert values in it, you can refer to the article on CREATE and INSERT statement.

Example 1

Write a query to extract a substring of 3 characters, starting for the 1st character for the CustName “Akash”.

SELECT SUBSTRING(CustName, 1, 3)
FROM Customers
WHERE CustName = ‘Akash’;

Output

Aka

Example 2

Write a query to extract a substring till the end of the string, starting for the 2nd character from the CustName “Akash”.

SELECT SUBSTRING(CustName, 2)
FROM Customers
WHERE CustName = ‘Akash’;

Output

kash

Example 3

Write a query to extract a substring of 3 characters, starting for the 2nd character for the CustName and order it according to the CustName.

SELECT CustName
FROM Customers
ORDER BY SUBSTRING(CustName, 2, 3);

Output:

anj
ita
kas
nuj
ona

USE SUBSTRING on Nested Queries

In this section of this article on a substring in SQL, let us understand how to use the SUBSTRING function on nested queries. To understand the same, let us consider the Customers table, we have considered above.

Example:

Write a query to extract all the domain from the CustEmail column on the Customers table.

SELECT
    CustEmail,
    SUBSTRING(
        CustEmail,
        CHARINDEX('@', CustEmail)+1,
        LEN(CustEmail)-CHARINDEX('@', CustEmail)
    ) Domain
FROM
   Customers
ORDER BY
    CustEmail;

Output:

CustEmail

Domain

anuj@abc.com

abc.com

akash@xyz.com

xyz.com

mitali@pqr.com

pqr.com

sonali@abc.com

abc.com

sanjay@xyz.com

xyz.com

Since the domain starts after the @ character,  we have used the CHARINDEX() function to search for the @character in the CustEmail column. Then the result of this function is used to determine the starting position and the length of the substring to be extracted. 

So, folks that’s how, you can use the SUBSTRING function in SQL, to retrieve data. With that, we come to an end of this article on SUBSTRING in SQL. I hope you found this article informative.

If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.

If you wish to learn Microsoft SQL Server and build a career in the relational databases, functions, and queries, variables etc domain, then check out our interactive, live-online SQL Training Course here, which comes with 24*7 support to guide you throughout your learning period.

Got a question for us? Please mention it in the comments section of this article and I will get back to you.

BROWSE COURSES
REGISTER FOR FREE WEBINAR SQL For Data Science Tutorial