The string comparison function in MySQL

Source: Internet
Author: User

As a basis, MySQL automatically converts numbers to strings, and vice versa.

mysql> SELECT 1+'1';
-> 2
mysql> SELECT CONCAT(2,' test');
-> '2 test'

To explicitly convert a number to a string, use the CAST () or CONCAT () function:

mysql> SELECT 38.8, CAST(38.8 AS CHAR);
-> 38.8, '38.8'
mysql> SELECT 38.8, CONCAT(38.8);
-> 38.8, '38.8'

CAST () preferred.

If a string function has been given a binary string as a parameter, the resulting string is also a binary string. A number converted to a string is treated as a binary string. This only has an effect on the results of the comparison.

In general, if any of the expressions in a string comparison are case-sensitive, performing comparisons is case-sensitive.

Expr like Pat [ESCAPE ' Escape-char ']

pattern matching, using SQL simple regular expression comparisons. Returns 1 (TRUE) or 0 (FALSE). If either expr or PAT is null, the result is null.

The pattern does not need to be a literal string. For example, you can be specified as a string expression or a table column.

The following two wildcard characters can be used with like in the pattern:

mysql> SELECT 'David!' LIKE 'David_';
-> 1
mysql> SELECT 'David!' LIKE '%D%v%';
-> 1

To verify the text instance of the wildcard character, you can place the escape characters in front of the character. If the escape character is not specified, it is assumed to be '.

mysql> SELECT 'David!' LIKE 'David_';
-> 0
mysql> SELECT 'David_' LIKE 'David_';
-> 1

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.