MySQL

MySQL SUBSTRING Function: The Complete Guide

intro

If you’ve been working with MySQL for a while, you might have heard about the SUBSTRING function. This function can help you extract a part of a string from a specific position. In this article, we'll be discussing everything you need to know about it.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
MySQL logo MYSQL 8.0+
THE MYSQL DATABASE VERSION 8 OR LATER

What is the SUBSTRING Function In MySQL?

The SUBSTRING function is used to extract a substring from a given string. It takes three arguments - the original string, the starting position, and the length of the substring. Here's the syntax of the SUBSTRING function in MySQL:

Copy
        
1 SUBSTRING(string, start, length)

How to Use the MySQL SUBSTRING Function

Now that you know what the SUBSTRING function is, let's see how to use it. Here's an example of using the SUBSTRING function to extract a substring from a string:

Copy
        
1 SELECT SUBSTRING('MySQL SUBSTRING Function', 7, 9);

In this example, we're extracting a substring starting from the 7th position and with a length of 9 characters. The output of this query would be "SUBSTRING".

Tips for Using the MySQL SUBSTRING Function

Here are a few tips to keep in mind when using the MySQL SUBSTRING function:

1. The Starting Position is Zero-Indexed

The starting position in the SUBSTRING function is zero-indexed. That means the first character of the string is at position 0, the second character is at position 1, and so on.

2. The Length Argument is Optional

The length argument in the SUBSTRING function is optional. If you don't provide the length argument, then the function will return the substring starting from the starting position to the end of the string.

3. The Starting Position and Length Can Be Negative

The starting position and length arguments in the SUBSTRING function can be negative. If the starting position is negative, then it counts from the end of the string. For example, a starting position of -1 would refer to the last character in the string. If the length argument is negative, then the function will return the substring up to the specified number of characters from the end of the string.

Common Mistakes to Avoid When Using the MySQL SUBSTRING Function

Here are a few common mistakes to avoid when using the MySQL SUBSTRING function:

1. Providing an Incorrect Starting Position

If you provide an incorrect starting position, then you'll get an incorrect substring. Make sure to double-check the starting position before using the SUBSTRING function.

2. Providing an Incorrect Length of the Substring

If you provide an incorrect length of the substring, then you'll get an incorrect substring. Make sure to double-check the length before using the SUBSTRING function.

3. Using the Wrong Function

Make sure that you're using the SUBSTRING function and not a similar function like LEFT or RIGHT. These functions have different syntaxes and functionality.

Dbvis download link img

Conclusion

The SUBSTRING function is a powerful tool that can help you extract substrings from a given string. By following the tips and avoiding common mistakes, you can use this function to its full potential. We hope this guide was helpful in understanding the SUBSTRING function in MySQL. Happy coding!

About the author
TheTable
TheTable

The Table by DbVisualizer is where we gather together to learn about and simplify the complexity of working with database technologies.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

Database Security: The Most Common Mistakes to Avoid

author Lukas Vileikis tags MARIADB MySQL SECURITY SQL 6 min 2025-06-09
title

How to Compare Datetimes in SQL: Multiple Approaches

author TheTable tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-06-04
title

The Complete Guide to CONCAT in SQL Queries: Syntax, Examples, and Best Practices

author Leslie S. Gyamfi tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-06-03
title

How Often Should SQL Transaction Logs Be Backed Up?

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-06-02
title

What Is a Database Catalog?

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-05-27
title

The Most Common MySQL Error Codes (and How to Avoid Them)

author Lukas Vileikis tags MARIADB MySQL SQL 5 min 2025-05-26
title

Check If A Table Exists in SQL: Multiple Approaches

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 6 min 2025-05-14
title

A Complete Guide to NOT EXISTS in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-05-12
title

SQL DROP TABLE IF EXISTS Statement: Complete Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-05-05
title

TRUNCATE vs DELETE: SQL Comparison of the Two Statements

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-04-23

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.