In SQL, you can use the SUBSTRING function to intercept a string. The syntax format is "SUBSTRING (string, 1, interception length)". The SUBSTRING function is used to grab part of a string, binary string, text, image, column, or expression containing a column.
The operating environment of this tutorial: windows7 system, mysql8.0 version, Dell G3 computer.
SUBSTRING ( expression, start, length )
Parameters
expression
String, binary string, text, image, column, or expression containing columns. Do not use expressions containing aggregate functions.
start
Integer or expression that can be implicitly converted to int, specifies the starting position of the substring, and the index starts from 1.
#length
An integer or an expression that can be implicitly converted to int, specifying the length of the substring. After testing, it was temporarily found that it can only be a non-negative number.
Return value
1. If expression is a supported binary data type, binary data is returned. We will not discuss this case for now.
2. If expression is a supported character data type, character data is returned.
(1) If the index of start|starts from 1, then the string interception starts from the first character of the expression, and from 2, it starts from the second character of the expression. And so on.
For example:
SELECT SUBSTRING('成都融资事业部',1,2)//结果:成都
Related recommendations: "mysql tutorial"
The above is the detailed content of How to intercept the first digits of a string in sql. For more information, please follow other related articles on the PHP Chinese website!