1. string truncation: left (str, length) mysqlselectleft (linuxidc.com, 3); + rows + left (linuxidc.com, 3) + ----------------------- + SQL + --------------------- + 2. string truncation: right (str, length) mysqlselectright (linuxidc.com, 3); + --
1. string truncation: left (str, length) mysqlselectleft ('linuxidc. com ', 3); + ----------------------- + left ('linuxidc. com ', 3) + ------------------------- + SQL + ----------------------- + 2. string truncation: right (str, length) mysqlselectright ('linuxidc. com ', 3); + --
1. string truncation: left (str, length) mysql> select left ('linuxidc. com ', 3); + ----------------------- + | left ('linuxidc. com ', 3) | + ----------------------- + | SQL | + ------------------------- + 2. string truncation: right (str, length) mysql> select right ('linuxidc. com ', 3); + ------------------------ + | right ('linuxidc. com ', 3) | + ------------------------ + | com | + -------------------------- + 3. string truncation: substring (str, pos); subst Ring (str, pos, len) 3.1 starts from the position of the string's 4th characters until the end. Mysql> select substring ('linuxidc. com ', 4); + ---------------------------- + | substring ('linuxidc. com ', 4) | + -------------------------- + | study.com | + -------------------------------- + 3.2 starts from the string's position of 4th characters and takes only 2 characters. Mysql> select substring ('linuxidc. com ', 4, 2); + --------------------------------- + | substring ('linuxidc. com ', 4, 2) | + ------------------------------- + | st | + ------------------------------------- + 3.3 starts from the string's position of 4th characters (reciprocal) until the end. Mysql> select substring ('linuxidc. com ',-4); + ----------------------------- + | substring ('linuxidc. com ',-4) | + ------------------------------- + |. com | + ------------------------------- + 3.4 starts from the string's 4th character position (reciprocal) and takes only 2 characters. Mysql> select substring ('linuxidc. com ',-4, 2); + ---------------------------------- + | substring ('linuxidc. com ',-4, 2) | + ------------------------------------ + |. c | + -------------------------------- + We noticed that in the substring (str, pos, len) function, pos can be negative, but len cannot be negative. 4. String truncation: substring_index (str, delim, count) 4.1 intercepts all the characters before the second. Mysql> select substring_index ('www .linuxidc.com ','. ', 2); + ---------------------------------------------- + | substring_index ('www .linuxidc.com ','. ', 2) | + ------------------------------------------------ + | www | + -------------------------------------------- + 4.2 intercepts the second one '. all characters After '(reciprocal. Mysql> select substring_index ('www .linuxidc.com ','. ',-2); + ----------------------------------------------- + | substring_index ('www .linuxidc.com ','. ',-2) | + ----------------------------------------------- + | com.cn | + Limit + 4.3 If the value specified by the delim parameter is not found in the string, returns the entire string mysql> select substring_index ('www .linuxidc.com ','. coc ', 1); + ------------------------------------------------- + | substring_index ('www .linuxidc.com ','. coc ', 1) | + ------------------------------------------------- + | www.linuxidc.com | + ----------------------------------------------- +
Original article address: mysql intercepts strings and thanks to the original author for sharing them.