MySQL string Intercept

Source: Internet
Author: User

Reproduced:

MySQL string intercept function:left(), right (), substring (), Substring_index (). There is also mid (), substr (). wheremid (), substr ( ) is equivalent to the substring () function,the function ofsubstring ( ) is very powerful and flexible.

1. string intercept:left(str, length)

Select Left (' 2017-11-14t16:00:00.000z ', 3) from DUAL
Results:201


2. string intercept:right(str, length)

Select Right (' 2017-11-14t16:00:00.000z ', 3) from DUAL
results:00Z

Instance:

# Two characters after querying a field
Select Right (LAST3, 2) as Last2 from Historydata limit 10;
# Update the two-bit characters from the expected field to another field
Update ' historydata ' Set ' Last2 ' =right (LAST3, 2);


3. string interception:substring (str, POS); substring (str, POS, len)

3.1 Starts at the 4-character position of the string until it ends.

Select substring (' 2017-11-14t16:00:00.000z ', 4) from DUAL
results:7-11-14t16:00:00.000z


3.2 Starts at the 1-character position of the string , taking only ten characters.

Select substring (' 2017-11-14t16:00:00.000z ', 1,10) from DUAL
results:2017-11-14


3.3 Starts from the 4 character position (reciprocal) of the string until it ends.

Select substring (' 2017-11-14t16:00:00.000z ',-4) from DUAL
results:000Z


3.4 is taken from the 4- character position (reciprocal) of the string and takes only 2 characters.

Select substring (' 2017-11-14t16:00:00.000z ', -4,2) from DUAL
Results:00


we notice that in function substring (str,pos, len) , pos can be negative, but len cannot take a negative value.

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

4.1 interception of the second '. ' all previous characters.

Select Substring_index (' www.example.com ', '. ', 2);
Results: www.example


4.2 interception of the second '. ' all characters after the (countdown).

Select Substring_index (' www.example.com ', '. ',-2);
results:example.com


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

Select Substring_index (' www.example.com ', '. CoC ', 1);
Results: www.example.com

MySQL string Intercept

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.