Application of string functions in MySQL notes

Source: Internet
Author: User

String operations are a very important component in programming, while string operations in MySQL databases are quite simple.

Note that all the following functions do not change the original data only when the modified result is returned.

Select a specified number of characters

Copy codeThe Code is as follows: mysql> select right ('nihao', 3 );
+ ------------------ +
| RIGHT ('nihao', 3) |
+ ------------------ +
| Hao |
+ ------------------ +
Row in set (0.00 sec)

The RIGHT () function indicates the three characters selected from the string from RIGHT to left.

Similarly, the LEFT () function

SUBSTRING_INDEX: truncates a string.

Copy codeThe Code is as follows: mysql> SELECT SUBSTRING_INDEX ('hh, MM, ss', ',', 2 );
+ ----------------------------------- +
| SUBSTRING_INDEX ('hh, MM, ss', ',', 2) |
+ ----------------------------------- +
| HH, MM |
+ ----------------------------------- +
Row in set (0.00 sec)

The first parameter in this function represents the content to be intercepted, and the second parameter represents the truncated content.

The last one is to intercept the nth number, 1 is to intercept the first comma, 2 is to intercept the second

SUBSTRING

Copy codeThe Code is as follows: mysql> select substring ('helloworld', 1, 5 );
+ ----------------------------- +
| SUBSTRING ('helloworld', 1, 5) |
+ ----------------------------- +
| Hello |
+ ----------------------------- +
Row in set (0.00 sec)

Intercept 1 ~ 5 content

UPPER string to uppercase
Copy codeThe Code is as follows: mysql> select upper ('hello ');
+ ---------------- +
| UPPER ('hello') |
+ ---------------- +
| HELLO |
+ ---------------- +
Row in set (0.00 sec)

LOWER-case LOWER string

Copy codeThe Code is as follows: mysql> select lower ('hello ');
+ ---------------- +
| LOWER ('hello') |
+ ---------------- +
| Hello |
+ ---------------- +
Row in set (0.00 sec)

REVERSE string

Copy codeThe Code is as follows: mysql> select reverse ('hello ');
+ ------------------ +
| REVERSE ('hello') |
+ ------------------ +
| Olleh |
+ ------------------ +
Row in set (0.00 sec)

LTRIM clear extra spaces on the left

Copy codeThe Code is as follows: mysql> select ltrim ('hello ');
+ ----------------------- +
| LTRIM ('hello') |
+ ----------------------- +
| Hello |
+ ----------------------- +
Row in set (0.00 sec)

In addition, RTRIM clears spaces on the right and TRIM clears spaces on both sides.

LENGTH returns the number of characters in the string.

Copy codeThe Code is as follows: mysql> select length ('HELO ');
+ ---------------- +
| LENGTH ('HELO') |
+ ---------------- +
| 4 |
+ ---------------- +
Row in set (0.00 sec)
Related Article

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.