Home >Database >Mysql Tutorial >mysql string conversion
MySQL is an open source and widely used relational database management system. In MySQL, there are many built-in string functions that make string manipulation and conversion easy.
This article will introduce the commonly used string conversion functions in MySQL, so that readers can operate string type data more skillfully.
1. Classification of string conversion functions
In MySQL, string conversion functions can be mainly divided into the following categories:
2. Examples
Let’s look at some examples of commonly used string conversion functions:
SELECT UPPER('hello, world!'); -- Output HELLO, WORLD!
SELECT LOWER('HELLO, WORLD!'); -- Output hello, world!
SELECT INITCAP ('hello, world!'); -- Output Hello, world!
SELECT CONVERT('Hello', 'gbk', 'utf8'); -- Output the GBK encoding format of "Hello"
SELECT CAST('123' AS SIGNED); -- Output 123
SELECT CONVERT('123', UNSIGNED); -- Output 123
SELECT REPLACE('hello, world!', 'world', 'MySQL'); -- Output "hello, MySQL!"
SELECT TRIM(' hello, world! '); -- Output "hello, world!"
SELECT LTRIM(' hello, world! '); -- Output "hello, world! "
SELECT RTRIM(' hello, world! '); -- Output " hello, world!"
SELECT SUBSTRING('hello, world!', 7); -- Output "world!"
SELECT LEFT('hello, world!', 5); -- Output "hello,"
SELECT RIGHT('hello, world!', 6); -- Output "world!"
SELECT CONCAT('hello', ', ', 'world!'); -- Output "hello, world!"
SELECT LENGTH('hello, world !'); -- Output 13
SELECT LOCATE('world', 'hello, world!'); -- Output 7
SELECT REPEAT('hello', 3); -- Output "hellohellohello"
3. Summary
This article provides a detailed introduction to some commonly used string conversion functions in MySQL, including case conversion, encoding conversion, type conversion, string replacement, string trimming, String interception, etc.
Proficient use of these functions can greatly improve the efficiency and accuracy of processing string data in MySQL.
It is worth noting that when using string functions, the length and encoding format of the string should be considered to avoid problems such as garbled characters.
I hope readers can gain a deeper understanding and mastery of the relevant knowledge of string conversion in MySQL through the introduction of this article.
The above is the detailed content of mysql string conversion. For more information, please follow other related articles on the PHP Chinese website!