Using SUBSTRING function in MySQL 8: A Practical Guide

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

The SUBSTRING function in MySQL is a powerful tool when it comes to string manipulation. It allows developers to extract a substring from a given string. Whether you need to work with user inputs, log files, or manipulate any kind of text data, understanding how to use SUBSTRING will undoubtedly come in handy. In this guide, we will delve into the basics of using SUBSTRING and gradually move on to more complex applications with various code examples.

Basic Usage of SUBSTRING

The SUBSTRING function can be called in multiple ways but has a consistent signature. Here is the simplest form:

SELECT SUBSTRING('string', start, length);

Example: Extract the first five characters of a string.

SELECT SUBSTRING('Hello World', 1, 5) AS extracted_string;

Output: Hello

In this example, ‘Hello’ is returned because we specified that extraction should begin at character position 1 and continue for 5 characters.

Extracting Text Without Specifying Length

When the length parameter is omitted, SUBSTRING will return all characters from the start position to the end of the string.

SELECT SUBSTRING('Hello World', 7) AS extracted_string;

Output: World

Negative Positioning

MySQL SUBSTRING also allows you to use negative indexing to specify positions. A negative start index specifies the starting position from the end of the string.

SELECT SUBSTRING('Hello World', -5) AS extracted_string;

Output: World

This conveniently extracts the last five characters.

Using SUBSTRING with Columns

Moving on from static examples to dynamic applications, consider a table users with a column email. Imagine you want to get the username before the @ symbol in the email addresses.

SELECT SUBSTRING(email, 1, LOCATE('@', email) - 1) AS username
FROM users;

This code utilizes LOCATE to find the position of the ‘@’ and SUBSTRING to extract the username.

Substrings with Expressions

MySQL allows the use of expressions within the SUBSTRING function. For instance, to dynamically adjust the start position:

SELECT SUBSTRING('Hello World', LENGTH('Hello ') + 1) AS extracted_string;

Output: World

This skips the ‘Hello ‘ portion of the string, thereby returning ‘World’.

Substring with REPLACE

We can combine SUBSTRING with other string functions such as REPLACE to transform text:

SELECT REPLACE(SUBSTRING('123456789', 2, 5), '4', 'x') AS replaced_substring;

Output: 23×56

This illustrates how you can extract part of a string and immediately apply further transformations to that extracted part.

Advanced Usage: SUBSTRING with Regular Expressions

MySQL 8 also allows regular expression matching with the REGEXP_SUBSTR function. Here’s an example of how you might use it:

SELECT REGEXP_SUBSTR('The price is 100 dollars', '\d+') AS price;

Output: 100

This returns the sequence of digits (‘100’) from the string, which is particularly handy for pattern extraction.

Dealing with Multibyte Characters

MySQL’s SUBSTRING function is multibyte safe, which means it will respect character boundaries when you’re dealing with multibyte encodings such as UTF-8. However, care must be taken as bytes and characters aren’t always the same, and for indexed locations, characters may not match with byte positions.

Here is an example using Multibyte characters:

SELECT SUBSTRING(_utf8mb4'Jalape\u00F1o', 1, 8) AS utf8mb4_string;

Output: Jalapeño

This example uses the utf8mb4 character set and correctly handles the multibyte ñ character.

Conclusion

The SUBSTRING function is an indispensable part of any SQL developer’s toolkit. Through the examples provided in this guide, we’ve seen how SUBSTRING can be leveraged to manipulate and manage text data effectively in MySQL 8. With basic and advanced techniques under your belt, you’ll find string handling both straightforward and powerful.