Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use the SUBSTRING () function in MySQL

Shulou Source: shulou.com Published: 2022-05-31 10:53:40 09月13日 Update

Xiaobian to share with you how to use SUBSTRING() function in MySQL, I hope you have gained something after reading this article, let's discuss it together!

SUBSTRING()

SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len), and SUBSTRING(str FROM pos FOR len) functions can all be used to return a substring starting at a specified position pos, where len indicates the length of the returned substring;pos = 0 indicates an empty string. For example:

SELECT SUBSTRING ('MySQL string function',-2) AS str1, SUBSTRING ('MySQL string function',-5, 3) AS str2;str1| str2 |-------+-----+ Function| string|

The positional parameter pos can be negative, in which case the returned substring starts with the pos character on the right side of the string. For example:

SELECT LEFT ('MySQL string functions', 5) AS str1, RIGHT ('MySQL string functions', 5) AS str2;str1| str2 |-----+---------+MySQL| string functions|

In addition, SUBSTR() and MID() functions are synonyms for SUBSTRING() function and also support the above four forms.

The LEFT(str,len) function returns len characters to the left of string str, and the RIGHT(str,len) function returns len characters to the right of string str. For example:

SELECT LEFT ('MySQL string functions', 5) AS str1, RIGHT ('MySQL string functions', 5) AS str2;str1| str2 |-----+---------+MySQL| string functions|

SUBSTRING_INDEX(str,delim,count) returns the substring before the count delimiter delim. If count is positive, count from the left and return all characters on the left; if count is negative, count from the right and return all characters on the right. For example:

SELECT SUBSTRING_INDEX ('Zhang San; Li Si; Wang Wu',';', 2) AS str1, SUBSTRING_INDEX ('Zhang San; Li Si; Wang Wu',';', -2) AS str2;str1 |str2 |--------|Li Si; Wang Wu| After reading this article, I believe you have a certain understanding of "how to use SUBSTRING() function in MySQL". If you want to know more about it, please pay attention to the industry information channel. Thank you for reading!

Tags: Function character string right Li Si Zhang San Wang Wu position article negative number delimiter parameter synonym finished form more positive number knowledge empty character industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Shulou Tech Info Redmi vpn