MySQL string function substring, string interception substring_index

MySQL string interception functions: left (), right (), substring (), substring_index (). There are also mid (), substr (). Among them, mid (), substr () is equivalent to the substring () function, and the function of substring () is very powerful and flexible.

1. String interception: left (str, length)

<yoon> select left('www.baidu.com',3);
+-------------------------+
| left('www.baidu.com',3) |
+-------------------------+
| www                     |
+-------------------------+

2. String interception: right (str, length)

<yoon> select right('www.baidu.com',3);
+--------------------------+
| right('www.baidu.com',3) |
+--------------------------+
| com                      |
+--------------------------+

3. String interception: substring (str, pos); substring (str, pos, len)

Intercept from the fourth position of the string until the end:

<yoon> select substring('www.baidu.com',4);
+------------------------------+
| substring('www.baidu.com',4) |
+------------------------------+
| .baidu.com                   |
+------------------------------+

Starting from the fourth position of the string, only two characters are intercepted:

<yoon> select substring('www.baidu.com',4,2);
+--------------------------------+
| substring('www.baidu.com',4,2) |
+--------------------------------+
| .b                             |
+--------------------------------+

Intercept from the fourth position (reciprocal) of the string until the end:

<yoon> select substring('www.baidu.com',-4);
+-------------------------------+
| substring('www.baidu.com',-4) |
+-------------------------------+
| .com                          |
+-------------------------------+

Starting from the fourth position (reciprocal) of the string, only two characters are intercepted:

<yoon> select substring ( ' www.baidu.com ' ,- 4 , 2 );
 + ----------------------------- ---- + 
| substring ( ' www.baidu.com ' ,- 4 , 2 ) | 
+ --------------------------- ------ + 
| .c | 
+ --------------------------------- + 
Note that in the function In substring (str, pos, len), pos can be negative, but len ​​cannot take negative values

4. String interception: substring_index (str, delim, count)

Intercept all characters before the second.

<yoon> select substring_index('www.baidu.com','.',2);
+----------------------------------------+
| substring_index('www.baidu.com','.',2) |
+----------------------------------------+
| www.baidu                              |
+----------------------------------------+

Truncate all characters after the second. (Countdown):

<yoon> select substring_index('www.baidu.com','.',-2);
+-----------------------------------------+
| substring_index('www.baidu.com','.',-2) |
+-----------------------------------------+
| baidu.com                               |
+-----------------------------------------+

If the value specified by the delim parameter is not found in the string, the entire string is returned

<yoon> select substring_index('www.baidu.com','coo',1);
+------------------------------------------+
| substring_index('www.baidu.com','coo',1) |
+------------------------------------------+
| www.baidu.com                            |
+------------------------------------------+

 

Guess you like

Origin www.cnblogs.com/hankyoon/p/12727723.html