MySQL string function: String Truncation

Source: Internet
Author: User
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 |

+ --------------------------------------------------- +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.