JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

MySQL SUBSTRING_INDEX function

Author:JIYIK Last Updated:2025/04/24 Views:

In this guide, we will understand the concept of substring_index function in MySQL. Such a concept allows the user to get a string of his choice from an array of different strings.

Let's take a closer look at this function.


SUBSTRING_INDEX in MySQL

First, let's understand a few substring_indeximportant concepts that are essential to using the function. They are: DELIMITERand CHARINDEX().

The delimiter is a composite symbol that serves as the main point where we want the string to break. The string will break at that point and the extracted string will be the string before or after that point, depending on our input.

Charindex()It is a function in MySQL that is used when we want to get the position of a specific substring in a string and return its position. Its parameters include (substring, string).

substring function code

The following code shows how the Substring_index function works. This code is equivalent to the SQL Server substring_index function of MySQL.

The following code should be run on SQL Server.

CREATE FUNCTION SUB_STRING
(
@STRING VARCHAR(8000),
@DELIMITER VARCHAR(1),
@POSITION INT
)
RETURNS VARCHAR(8000)
AS
BEGIN
IF @POSITION = 0
BEGIN
    RETURN SUBSTRING(@STRING,0,CHARINDEX(@DELIMITER,@STRING))
END
IF @POSITION = 1
BEGIN
    RETURN SUBSTRING(@STRING,CHARINDEX(@DELIMITER,@STRING)+1,LEN(@STRING))
END
RETURN @STRING
END;

Let's understand the code.

We have created a function namely SUB_STRING and inside the function we have parameters @string (the string from which the substring will be taken), @delimiter and @position (which has two values ​​0 or 1).

@PositionThis is a parameter. If it is set to 0, it will get from the starting index of the string to the separation point. If it is set to 1, it will get from 1 point before the separation point until the last string.

The Begin clause indicates the beginning of a code block, and the End clause indicates the end of a code block.

In the first IF condition, position is set to 0. In the substring function, we get the string to be extracted from the 0 index, i.e., Charindex()function.

This returns the substring from the 0 index to the delimiting point.

Example 1

Let us understand this with the following example.

SELECT dbo.SUB_STRING('ahsanali@email.com','@',0)

substring 0

As you can see, it returns the substring from index 0 to the delimiter point.

The second IF condition sets the position to 1. In the substring function, we have the Charindex() function with the +1 condition which will get the substring which is the 1 index before the delimiter point till the last .

Example 2

Let us understand further with the help of another example.

SELECT dbo.SUB_STRING('ahsanali@email.com','@',1)

substring 1

As you can see, the substring it returns is the 1 index before the delimiting point and goes all the way to the end of the string.

Also, one more thing is that if you @position(0 or 1)give a wrong value at , it will return the complete string.

SELECT dbo.SUB_STRING('ahsanali@email.com','&',3)

wrong value

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Get the version in MySQL

Publish Date:2025/04/24 Views:169 Category:MySQL

In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T

Full Join in MySQL

Publish Date:2025/04/24 Views:70 Category:MySQL

This article aims to explore how to perform a full join or full outer join in MySQL. Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_id and student_

Grouping by month in MySQL

Publish Date:2025/04/24 Views:166 Category:MySQL

In this article, we will learn how to group values ​​by month in MySQL database. Businesses and organizations have to find user or customer data based on purchase or usage trends over a few months. If a particular business achieves its

Enabling the slow query log in MySQL

Publish Date:2025/04/24 Views:177 Category:MySQL

Today, we will enable MySQL in MySQL using MySQL shell on Windows and Ubuntu 20.04 slow_query_log . For this tutorial, we are using MySQL version 8.0 and Ubuntu 20.04. MySQL slow_query_log MySQL slow_query_log contains SQL statements that t

Update multiple tables in MySQL with one query

Publish Date:2025/04/24 Views:65 Category:MySQL

In some cases, users want to update logically related tables at the same time. These logically related tables are linked to each other through some attributes. Advantages of updating multiple tables in one MySQL query Similar attributes in

Checking MySQL version in macOS

Publish Date:2025/04/24 Views:60 Category:MySQL

In this article, we aim to explore how to check the current version of MySQL on macOS. Checking MySQL version in macOS When trying to figure out the version, you must follow these steps. Each time a person logs into the MySQL server, the ve

Common table expressions in MySQL

Publish Date:2025/04/24 Views:168 Category:MySQL

This article aims to understand how to use common table expressions in MySQL. Most data analysts need to store the results of different queries in order to merge them with a separate query. With the help of common tables, expressions can ma

Sorting by date in MySQL

Publish Date:2025/04/24 Views:156 Category:MySQL

This article aims to understand how to sort values ​​by date in MySQL. Most of the businesses and organizations that use MySQL for data analysis or data visualization need to sort different table values ​​of their users based on dat

Sort MySQL data alphabetically

Publish Date:2025/04/24 Views:153 Category:MySQL

In this article, we aim to explore how to sort data alphabetically in MySQL database. Sorting is the ordering of elements or values ​​in an array or column based on a particular criteria. In this tutorial, we will set the criteria as al

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial