Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the SUBSTRING () function in MySQL

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report