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.
mysql string functions are:
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 | +---------------------+
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 | +--------------------+
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 | +-----------------------+
If any parameter is null, the function returns null
mysql> select concat('My',null,'QL');+------------------------+ | concat('My',null,'QL') | +------------------------+ | NULL | +------------------------+
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 | +----------------------+
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 | +-------------------------------------------------------+
! ! 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 | +--------------------------+
6. LENGTH(str): Returns the storage length of the string
mysql> select length('text'),length('你好');+----------------+------------------+ | length('text') | length('你好') | +----------------+------------------+ | 4 | 6 | +----------------+------------------+
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 | +---------------------+-----------------------+
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 | +--------------------------+
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 | +-------------------+
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???? |+-------------------+
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 | +-------------------------------------+
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 | +-----------------------------------+
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 | +-----------+-----------+
14. REPEAT(str, count): Repeat the string str count times Then return
mysql> select repeat('MySQL',3);+-------------------+ | repeat('MySQL',3) | +-------------------+ | MySQLMySQLMySQL | +-------------------+
15. REVERSE(str): Reverse the string str and return
mysql> select reverse('abcdef');+-------------------+ | reverse('abcdef') | +-------------------+ | fedcba | +-------------------+
##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 | +-------------------------+----------------------+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 | +---------------------+--------------------------------+
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 spacesmysql> select space(3);+----------+ | space(3) | +----------+ | | +----------+###
19、LEFT(str, len):返回最左边的len长度的子串
mysql> select left('chinaitsoft',5);+-----------------------+ | left('chinaitsoft',5) | +-----------------------+ | china | +-----------------------+
20、RIGHT(str, len):返回最右边的len长度的子串
mysql> select right('chinaitsoft',5);+------------------------+ | right('chinaitsoft',5) | +------------------------+ | tsoft | +------------------------+
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 | +-------------------------+-------------------------+
相关学习推荐: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!

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
