Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The MySQL string intercepts the SUBSTRING() function


May 15, 2021 MySQL


Table of contents


MySQL string interception related functions:

1, intercept the string from the left
left(str, length)
Description: left (intercepted field, intercepted length)
Cases:
select left(content,200) as abstract from my_content_t 
2, intercept the string from the right
right(str, length)
Description: right (intercepted field, intercepted length)
Cases:
select right(content,200) as abstract from my_content_t 
3, intercept the string
substring(str, pos)
substring(str, pos, length)
Description: substring (intercepted field, intercepted from first place)
substring (intercepted field, intercepted from first place, intercept length)

Cases:

select substring(content,5) as abstract from my_content_t 
select substring(content,5,200) as abstract from my_content_t 

(Note: If the number of digits is negative, such as -5, it is the length from the last countdown to the end of the string or the length of the interception)
4, by keyword to intercept the string
substring_index(str,delim,count)
Description: substring_index (intercepted fields, separators, number of keyword appearances)

Cases:

select substring_index("www.w3cschool.cn",".",2) as abstract from wiki_user 

Results: www.w3cschool
(Note: If the number of times the keyword appears is negative, such as -2, it is the last countdown to the end of the string)

Introduction to functions:

SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FOR len)

Formats without len parameters return a substring from the string str, starting at the position pos. T he format with the len parameter returns a substring the same length as the len character from the string str, starting at the position pos. U se the FROM format as the standard SQL syntax. Y ou may also use a negative value for pos. I f so, the position of the substring starts with the pos character at the end of the string, not at the beginning of the string. You can use a negative value for pos in functions in the following format.