Application of string functions in MySQL notes _ MySQL

Source: Internet
Author: User
MySQL notes string function application bitsCN.com

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

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.

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

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

Mysql> select upper ('Hello ');
+ ---------------- +
| UPPER ('Hello') |
+ ---------------- +
| HELLO |
+ ---------------- +
Row in set (0.00 sec)

LOWER-case LOWER string

Mysql> select lower ('Hello ');
+ ---------------- +
| LOWER ('Hello') |
+ ---------------- +
| Hello |
+ ---------------- +
Row in set (0.00 sec)

REVERSE string

Mysql> select reverse ('Hello ');
+ ------------------ +
| REVERSE ('Hello') |
+ ------------------ +
| Olleh |
+ ------------------ +
Row in set (0.00 sec)

LTRIM clear extra spaces on the left

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.

Mysql> select length ('helo ');
+ ---------------- +
| LENGTH ('helo') |
+ ---------------- +
| 4 |
+ ---------------- +
Row in set (0.00 sec)

BitsCN.com

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.