Home >Database >Mysql Tutorial >How to intercept the first digits of a string in mysql
Mysql method to intercept the first digits of a string: 1. Use the LEFT() function, the syntax "left (intercepted string, interception length)"; 2. Use the SUBSTRING() function, the syntax "substring( The intercepted string, 1, interception length)".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Mysql method of intercepting the first digits of a string:
1. Use the LEFT() function
LEFT() function can intercept the string starting from the left
Usage: left(str, length)
, that is: left (intercepted string, intercepted length)
[Example] Use the LEFT function to return the left character in the string
mysql> SELECT LEFT('MySQL',2); +-----------------+ | LEFT('MySQL',2) | +-----------------+ | My | +-----------------+ 1 row in set (0.04 sec)
It can be seen from the running results that the length of the character string "MySQL" starting from the left is returned substring, the result is "My".
2. Use the SUBSTRING() function
SUBSTRING() function to intercept a string of a specific length
Usage:
substring(str, pos, length)
, that is: substring (the intercepted string, starting from which number, intercepting the length)
When SUBSTRING When the second parameter pos
of the () function is 1, a string of a specific length is intercepted from the beginning.
It is also possible to use a negative value for pos
. If so, the position of the substring starts at the pos
th character from the end of the string, that is, the penultimate pos
character, rather than the beginning of the string.
[Example] Use the SUBSTRING function to obtain the substring at the specified position,
mysql> SELECT SUBSTRING('computer',1,3) AS col1, -> SUBSTRING('computer',3,4) AS col2, -> SUBSTRING('computer',-3) AS col3, -> SUBSTRING('computer',-5,3) AS col4; +--------+------+------+------+ | col1 | col2 | col3 | col4 | +------+------+------+------+ | com | mput | ter | put | +--------+------+------+------+ 1 row in set (0.00 sec)
[Related recommendations: mysql video tutorial]
The above is the detailed content of How to intercept the first digits of a string in mysql. For more information, please follow other related articles on the PHP Chinese website!