MySQL string truncation functions: left (), right (), substring (), substring_index (). There are also mid () and substr (). Here, mid () and substr () are equivalent to substring () functions. substring () functions are very powerful and flexible. 1. String truncation: left (str, length) mysqlselectleft (www.68idc.cn,
MySQL string truncation functions: left (), right (), substring (), substring_index (). There are also mid () and substr (). Here, mid () and substr () are equivalent to substring () functions. substring () functions are very powerful and flexible. 1. String truncation: left (str, length) mysql select left (www.68idc.cn,
MySQL string truncation functions: left (), right (), substring (), substring_index (). There are also mid () and substr (). Here, mid () and substr () are equivalent to substring () functions. substring () functions are very powerful and flexible.
1. String truncation: left (str, length)
Mysql> select left ('www .68idc.cn ', 3 );
+ ------------------------- +
| Left ('www .68idc.cn ', 3) |
+ ------------------------- +
| SQL |
+ ------------------------- +
2. String truncation: right (str, length)
Mysql> select right ('www .68idc.cn ', 3 );
+ -------------------------- +
| Right ('www .68idc.cn ', 3) |
+ -------------------------- +
| Com |
+ -------------------------- +
3. String truncation: substring (str, pos); substring (str, pos, len)
3.1 starts from the string's 4th character position and ends.
Mysql> select substring ('www .68idc.cn ', 4 );
+ ------------------------------ +
| Substring ('www .68idc.cn ', 4) |
+ ------------------------------ +
| Study.com |
+ ------------------------------ +
3.2 starts from the string's 4th character position and takes only 2 characters.
Mysql> select substring ('www .68idc.cn ', 4, 2 );
+ --------------------------------- +
| Substring ('www .68idc.cn ', 4, 2) |
+ --------------------------------- +
| St |
+ --------------------------------- +
3.3 starts from the position (reciprocal) of the string's 4th characters until the end.
Mysql> select substring ('www .68idc.cn ',-4 );
+ ------------------------------- +
| Substring ('www .68idc.cn ',-4) |
+ ------------------------------- +
|. Com |
+ ------------------------------- +
3.4 starts from the string's 4th character position (reciprocal) and takes only 2 characters.
Mysql> select substring ('www .68idc.cn ',-4, 2 );
+ ---------------------------------- +
| Substring ('www .68idc.cn ',-4, 2) |
+ ---------------------------------- +
|. C |
+ ---------------------------------- +
We noticed that in the substring (str, pos, len) function, pos can be a negative value, but len cannot be a negative value.
4. String truncation: substring_index (str, delim, count)
4.1 intercept all characters before the second.
Mysql> select substring_index ('www .68idc.cn ','. ', 2 );
+ ------------------------------------------------ +
| Substring_index ('www .68idc.cn ','. ', 2) |
+ ------------------------------------------------ +
| Www |
+ ------------------------------------------------ +
4.2 intercept all characters after the second '.' (reciprocal.
Mysql> select substring_index ('www .68idc.cn ','. ',-2 );
+ ------------------------------------------------- +
| Substring_index ('www .68idc.cn ','. ',-2) |
+ ------------------------------------------------- +
| Com.cn |
+ ------------------------------------------------- +
4.3 If the delim parameter value cannot be found in the string, the entire string is returned.
Mysql> select substring_index ('www .68idc.cn ','. coc', 1 );
+ --------------------------------------------------- +
| Substring_index ('www .68idc.cn ','. coc', 1) |
+ --------------------------------------------------- +
| Www.68idc.cn |
+ --------------------------------------------------- +