Home Database Mysql Tutorial What are the mysql string functions?

What are the mysql string functions?

Jun 29, 2020 am 11:37 AM
mysql String functions

mysql string functions include: 1. LOWER, convert the string parameter value to all lowercase letters and return; 2. UPPER, convert the string parameter value to all uppercase letters and return; 3. CONCAT, Return multiple string parameters concatenated end to end; 4. SUBSTR, starting from the specified position pos in the source string str.

What are the mysql string functions?

mysql string functions are:

What are the mysql string functions?

1, LOWER(column|str): Convert the string parameter value to all lowercase letters and return

mysql> select lower('SQL Course');+---------------------+
| lower('SQL Course') |
+---------------------+
| sql course          |
+---------------------+
Copy after login

2, UPPER(column| str): Convert the string parameter value to all uppercase letters and return

mysql> select upper('Use MYsql');+--------------------+
| upper('Use MYsql') |
+--------------------+
| USE MYSQL          |
+--------------------+
Copy after login

3, CONCAT(column|str1, column| str2,...): Return after concatenating multiple string parameters end to end

mysql> select concat('My','S','QL');+-----------------------+
| concat('My','S','QL') |
+-----------------------+
| MySQL                 |
+-----------------------+
Copy after login

If any parameter is null, the function returns null

mysql> select concat('My',null,'QL');+------------------------+
| concat('My',null,'QL') |
+------------------------+
| NULL                   |
+------------------------+
Copy after login

If the parameter is a number, Then it will be automatically converted into a string

mysql> select concat(14.3,'mysql');+----------------------+
| concat(14.3,'mysql') |
+----------------------+
| 14.3mysql            |
+----------------------+
Copy after login

4. CONCAT_WS(separator,str1,str2,...):Convert multiple string parameters to the given The separator separator is connected end to end and returns

mysql> select concat_ws(';','First name','Second name','Last name');+-------------------------------------------------------+
| concat_ws(';','First name','Second name','Last name') |
+-------------------------------------------------------+
| First name;Second name;Last name                      |
+-------------------------------------------------------+
Copy after login

! ! That is, the first item in the function parentheses is used to specify the separator

5, SUBSTR(str,pos[,len]): From the specified position in the source string str pos starts taking a string and returns

Note:

①len specifies the length of the substring. If omitted, it will be taken to the end of the string; a negative value of len means starting from the source string The tail begins to pick up.

 ②Function SUBSTR() is a synonym of function SUBSTRING().

mysql> select substring('hello world',5);+----------------------------+
| substring('hello world',5) |
+----------------------------+
| o world                    |
+----------------------------+mysql> select substr('hello world',5,3);+---------------------------+
| substr('hello world',5,3) |
+---------------------------+
| o w                       |
+---------------------------+mysql> select substr('hello world',-5);+--------------------------+
| substr('hello world',-5) |
+--------------------------+
| world                    |
+--------------------------+
Copy after login

6. LENGTH(str): Returns the storage length of the string

mysql> select length('text'),length('你好');+----------------+------------------+
| length('text') | length('你好')   |
+----------------+------------------+
|              4 |                6 |
+----------------+------------------+
Copy after login

Note: The storage length of the string depends on the encoding method. Different ('Hello': utf8 is 6, gbk is 4)

7, CHAR_LENGTH(str): Return string The number of characters in

mysql> select char_length('text'),char_length('你好');+---------------------+-----------------------+
| char_length('text') | char_length('你好')   |
+---------------------+-----------------------+
|                   4 |                     2 |
+---------------------+-----------------------+
Copy after login

8. INSTR(str, substr): Return the substring substr from the source string str The position of an occurrence

mysql> select instr('foobarbar','bar');+--------------------------+
| instr('foobarbar','bar') |
+--------------------------+
|                        4 |
+--------------------------+
Copy after login

9. LPAD(str, len, padstr): Padding the given left side of the source string character padstr to the specified length len, and return the filled string

mysql> select lpad('hi',5,'??');+-------------------+
| lpad('hi',5,'??') |
+-------------------+
| ???hi             |
+-------------------+
Copy after login

10. RPAD(str, len, padstr): Padding the given character padstr to the specified length len on the right side of the source string, and returning the filled string

mysql> select rpad('hi',6,'??');+-------------------+| rpad('hi',6,'??') |+-------------------+| hi????            |+-------------------+
Copy after login


11. TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str):

Remove both ends, prefixes or The suffix character remstr is returned;

If remstr is not specified, the spaces at both ends of str will be removed; if BOTH, LEADING, and TRAILING are not specified, the default is BOTH.

mysql> select trim('  bar  ');+-----------------+
| trim('  bar  ') |
+-----------------+
| bar             |
+-----------------+mysql> select trim(leading 'x' from 'xxxbarxxx');+------------------------------------+
| trim(leading 'x' from 'xxxbarxxx') |
+------------------------------------+
| barxxx                             |
+------------------------------------+mysql> select trim(both 'x' from 'xxxbarxxx');+---------------------------------+
| trim(both 'x' from 'xxxbarxxx') |
+---------------------------------+
| bar                             |
+---------------------------------+mysql> select trim(trailing 'xyz' from 'barxxyz');+-------------------------------------+
| trim(trailing 'xyz' from 'barxxyz') |
+-------------------------------------+
| barx                                |
+-------------------------------------+
Copy after login

12. REPLACE(str, from_str, to_str): Find all substrings form_str (case sensitive) in the source string str, and find them Replace it with the replacement string to_str. Return the replaced string

mysql> select replace('www.mysql.com','w','Ww');+-----------------------------------+
| replace('www.mysql.com','w','Ww') |
+-----------------------------------+
| WwWwWw.mysql.com                  |
+-----------------------------------+
Copy after login

13, LTRIM (str), RTRIM (str): Remove the left or right side of the string Spaces (left justified, right justified)

mysql> SELECT  ltrim('   barbar   ') rs1, rtrim('   barbar   ') rs2;+-----------+-----------+
| rs1       | rs2       |
+-----------+-----------+
| barbar    |    barbar |
+-----------+-----------+
Copy after login

14. REPEAT(str, count): Repeat the string str count times Then return

mysql> select repeat('MySQL',3);+-------------------+
| repeat('MySQL',3) |
+-------------------+
| MySQLMySQLMySQL   |
+-------------------+
Copy after login

15. REVERSE(str): Reverse the string str and return

mysql> select reverse('abcdef');+-------------------+
| reverse('abcdef') |
+-------------------+
| fedcba            |
+-------------------+
Copy after login

##16, CHAR(N,... [USING charset_name]): Interpret each parameter N as an integer (character encoding), and return each A string consisting of characters corresponding to an integer (NULL values ​​are ignored).

mysql> select char(77,121,83,81,'76'),char(77,77.3,'77.3');+-------------------------+----------------------+
| char(77,121,83,81,'76') | char(77,77.3,'77.3') |
+-------------------------+----------------------+
| MySQL                   | MMM                  |
+-------------------------+----------------------+
Copy after login

By default, the function returns a binary string. If you want to return a string for a specific character set, use the using option

mysql> SELECT charset(char(0x65)), charset(char(0x65 USING utf8));+---------------------+--------------------------------+
| charset(char(0x65)) | charset(char(0x65 USING utf8)) |
+---------------------+--------------------------------+
| binary              | utf8                           |
+---------------------+--------------------------------+
Copy after login

17. FORMAT(X,D[,locale]): Format the number X

  • in the format '#,

    ,

    .##' D specifies the number of decimal places
  • locale specifies the national language (the default locale is en_US)


mysql> SELECT format(12332.123456, 4), format(12332.2,0); ---------------------------------- ------------------ -
| format(12332.123456, 4) | format(12332.2,0) |
----------------------- --- ----------------
| 12,332.1235 | 12,332 |
----------------------- ------------------- mysql> SELECT format(12332.2,2,'de_DE'); --------------- ------------
| format(12332.2,2,'de_DE') |
-------------------- -------
| 12.332,20                 |
---------------------------

18. SPACE(N):

Returns a string consisting of N spaces

mysql> select space(3);+----------+
| space(3) |
+----------+
|          |
+----------+
Copy after login

###

19、LEFT(str, len):返回最左边的len长度的子串

mysql> select left('chinaitsoft',5);+-----------------------+
| left('chinaitsoft',5) |
+-----------------------+
| china                 |
+-----------------------+
Copy after login

20、RIGHT(str, len):返回最右边的len长度的子串

mysql> select right('chinaitsoft',5);+------------------------+
| right('chinaitsoft',5) |
+------------------------+
| tsoft                  |
+------------------------+
Copy after login

21、STRCMP(expr1,expr2):如果两个字符串是一样的则返回0;如果第一个小于第二个则返回-1;否则返回1

mysql> select strcmp('text','text');+-----------------------+
| strcmp('text','text') |
+-----------------------+
|                     0 |
+-----------------------+mysql> SELECT strcmp('text', 'text2'),strcmp('text2', 'text');+-------------------------+-------------------------+
| strcmp('text', 'text2') | strcmp('text2', 'text') |
+-------------------------+-------------------------+
|                      -1 |                       1 |
+-------------------------+-------------------------+
Copy after login

相关学习推荐:mysql视频教程

The above is the detailed content of What are the mysql string functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL: Key Features and Capabilities Explained MySQL: Key Features and Capabilities Explained Apr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

See all articles