Hello everyone! I am Mr. Tony who only talks about techniques and doesn’t cut hair. A database function is a module that has some functionality, can receive zero or more input values, and return an output value. MySQL provides us with many system functions for processing and analyzing data. This article introduces 10 commonly used string functions and other related functions. CONCAT() SELECT CONCAT('MySQL', 'string', 'function') AS str; str | --------------+ MySQL string functions | If any parameter in this function is NULL, the return result is NULL. For example: SELECT CONCAT('MySQL', NULL, 'function') AS str; str| ---+ | For string constants, we can also directly concatenate them together. For example: SELECT 'MySQL' 'string' 'function' AS str; str | --------------+ MySQL string functions | The above method can only be used to connect string constants, not to connect field values.
In addition to the CONCAT(str1,str2,…)) function, SELECT CONCAT_WS('-', 'MySQL', NULL, 'string') AS str1, CONCAT_WS(NULL, 'MySQL', 'string') AS str2; str1 |str2| -----------+----+ MySQL-String | | LOWER() SELECT LOWER('MySQL string function') AS str1, LCASE('MySQL string function') AS str2; str1 |str2 | --------------+--------------+ mysql string function |mysql string function | MySQL case conversion functions do not support binary strings (BINARY, VARBINARY, BLOB) and can be converted to non-binary strings before processing. For example: SELECT LOWER(BINARY 'MySQL string function') AS str1, LOWER(CONVERT(BINARY 'MySQL string function' USING utf8mb4)) AS str2; str1 |str2 | -------------------+-------------+ MySQL string function | UPPER() SELECT UPPER('MySQL string function') AS str1, UCASE('MySQL string function') AS str2; str1 |str2 | --------------+--------------+ MYSQL string function |MYSQL string function | LENGTH() SELECT LENGTH('MySQL string function') AS len1, OCTET_LENGTH('MySQL string function') AS len2; len1|len2| ----+----+ 20| 20| In utf8mb4 encoding, a Chinese character occupies 3 bytes. In addition, SELECT CHAR_LENGTH('MySQL string function') AS len1, CHARACTER_LENGTH('MySQL string function') AS len2; len1|len2| ----+----+ 10| 10| The SELECT BIT_LENGTH('MySQL string function') AS len; len| ---+ 160| A byte contains 8 bits. SUBSTRING() SELECT SUBSTRING('MySQL string function', -2) AS str1, SUBSTRING('MySQL string function', -5, 3) AS str2; str1 |str2 | ------+------+ function|string| The position parameter pos can be a negative number, in which case the returned substring starts from the posth character on the right side of the string. For example: SELECT LEFT('MySQL string function',5) AS str1, RIGHT('MySQL string function',5) AS str2; str1 |str2 | -----+---------+ MySQL|String Functions| In addition, SELECT LEFT('MySQL string function',5) AS str1, RIGHT('MySQL string function',5) AS str2; str1 |str2 | -----+---------+ MySQL|String Functions| SELECT SUBSTRING_INDEX('张三;李四;王五', ';', 2) AS str1, SUBSTRING_INDEX('张三;李四;王五', ';', -2) AS str2; str1 |str2 | --------+--------+ Zhang San; Li Si|Li Si; Wang Wu| TRIM() SELECT TRIM('MySQL string function') AS str1, TRIM('-' FROM '--MySQL string function--') AS str2; str1 |str2 | --------------+--------------+ MySQL string functions |MySQL string functions | SELECT TRIM(LEADING ' ' FROM 'MySQL string function') AS str1, TRIM(TRAILING '-' FROM '--MySQL string function--') AS str2; str1 |str2 | ----------------+----------------+ MySQL string functions |--MySQL string functions | LPAD()/RPAD() SELECT LPAD(123, 6, '0') AS str1, LPAD(123, 2, '0') AS str2, RPAD(123, 6, '0') AS str1, RPAD(123, 2, '0') AS str1; str1 |str2|str1 |str1| ------+----+------+----+ 000123|12 |123000|12 | When the length of the string str is greater than len, it is equivalent to truncating the string from the right. In addition, the SELECT REPEAT('🔥', 5) AS str; str | ----------+ 🔥🔥🔥🔥🔥| INSTR() select INSTR('MySQL string function', 'string') AS index1, INSTR('MySQL string function', 'date') AS index2, INSTR('MySQL string function', '') AS index3, INSTR('MySQL string function', null) AS index4; index1|index2|index3|index4| ------+------+------+------+ 6| 0| 1| | In addition, the SELECT LOCATE('S','MySQL Server', 5) AS ind; ind| ---+ 7| The FIELD(str,str1,str2,str3,…) function returns the position where the string str appears in the subsequent string list, and returns 0 if it is not found. For example: SELECT FIELD('Li Si', 'Zhang San', 'Li Si', 'Wang Wu') AS ind; ind| ---+ 2| The FIND_IN_SET(str,strlist) function returns the position where the string str appears in the list string strlist. strlist consists of N substrings separated by commas. For example: SELECT FIND_IN_SET('Li Si', 'Zhang San, Li Si, Wang Wu') AS ind; ind| ---+ 2| REPLACE() The SELECT REPLACE('MySQL string function', 'string', 'date') AS str1, REPLACE('MySQL string function', 'string', '') AS str2; str1 |str2 | ------------+---------+ MySQL date function |MySQL function | In addition, SELECT INSERT('MySQL string function', 6, 3, 'date') AS str; str | ------------+ MySQL Date Functions | REVERSE() SELECT REVERSE('Shanghai tap water comes from the sea')='Shanghai tap water comes from the sea' AS "Palindrome"; Palindrome| ----+ 1| If you want to learn more about MySQL, you can refer to this article. This concludes this article on the top ten commonly used string functions in MySQL. For more relevant MySQL string function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Solution to installing vim in docker container
>>: Unicode signature BOM detailed description
1. Background The company's projects have alw...
Download image Selecting a MySQL Image docker sea...
Table of contents Explanation of v-text on if for...
If you set the table-layer:fixed style for a tabl...
The above article has temporarily concluded my int...
Table of contents Purpose npm init and package.js...
Table of contents 1. What is Ts 2. Basic Grammar ...
Table of contents Overview Performance.now Consol...
Table of contents 1. Overview 1. Introduction to ...
introduction I used postgresql database for some ...
I used ECharts when doing a project before. Today...
In MySQL 8.0.18, a new Hash Join function was add...
1. Error error connecting to master 'x@xxxx:x...
Table of contents Preface 1. First completely uni...
The notepad program is implemented using the thre...