当前位置:网站首页>String function of MySQL function summary
String function of MySQL function summary
2022-07-23 14:48:00 【Operation and maintenance home】
The string function is mainly used to process string data in the database .
mysqlThe string function in has the function of calculating string length 、 String merge function 、 String replacement function 、 String comparison function 、 Find the specified string position function, etc .

A function that counts the number of characters in a string
CHAR_LENGTH(str) Function evaluates the string str Number of characters contained . A multibyte character is counted as a single character .
mysql> select char_length('date'), char_length('yunweijia'), char_length(' Operation and maintenance home ');
+---------------------+--------------------------+--------------------------+
| char_length('date') | char_length('yunweijia') | char_length(' Operation and maintenance home ') |
+---------------------+--------------------------+--------------------------+
| 4 | 9 | 3 |
+---------------------+--------------------------+--------------------------+
1 row in set (0.00 sec)
mysql>
A function that calculates the length of a string
LENGTH(str) The return value is the byte length of the string , Use utf8 When encoding character sets , A Chinese character is three bytes , A number or a letter 1 byte ;
mysql> select length('date'), length('yunweijia'), length(' Operation and maintenance home ');
+----------------+---------------------+---------------------+
| length('date') | length('yunweijia') | length(' Operation and maintenance home ') |
+----------------+---------------------+---------------------+
| 4 | 9 | 9 |
+----------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql>
Merge string functions CONCAT(s1,s2,…)、CONCAT_WS(x,s1,s2,…)
concat(s1,s2,...) The return result is the string generated by the connection parameter , There may be one or more parameters .
If you have a parameter that is null, The return value is null.
If all parameters are non binary strings , The result is a non binary string .
If the argument contains any binary string , The result is a binary string .
mysql> select concat('mysql 8.0', 'yunweijia'), concat('heihei', null, '666');
+----------------------------------+-------------------------------+
| concat('mysql 8.0', 'yunweijia') | concat('heihei', null, '666') |
+----------------------------------+-------------------------------+
| mysql 8.0yunweijia | NULL |
+----------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql>
concat_ws(x,s1,s2,...) in ,x The separator is , The position of the separator is placed between the two strings to be connected . The separator can be a string , It can also be other parameters .
If the separator is null, The result is null. This function will Ignore null value .
mysql> select concat_ws('-', 'mysql', '8.0', 'ywj'), concat_ws('@', 'one', null, 'two');
+---------------------------------------+------------------------------------+
| concat_ws('-', 'mysql', '8.0', 'ywj') | concat_ws('@', 'one', null, 'two') |
+---------------------------------------+------------------------------------+
| mysql-8.0-ywj | [email protected] |
+---------------------------------------+------------------------------------+
1 row in set (0.00 sec)
mysql>
A function that replaces a string INSERT(s1,x,len,s2)
insert(s1,x,len,s2) Return string s1, Its substring starts at x Position and string s2 To replace len character . If x Exceeds string length , The return value is the original string . If len The length of is greater than the length of other strings , From the position x Start replacing . If any parameter is null, The return value is null.
mysql> select insert('yunweijia', 2, 3, 'aaaaaaaa') as coll, insert('yunweijia', -1, 3, 'bbbbbbbb') as coll2, insert('yunweijia', 2, 99, 'ccccccccc') as coll3;
+----------------+-----------+------------+
| coll | coll2 | coll3 |
+----------------+-----------+------------+
| yaaaaaaaaeijia | yunweijia | yccccccccc |
+----------------+-----------+------------+
1 row in set (0.00 sec)
mysql>
The first function insert('yunweijia', 2, 3, 'aaaaaaaa') take yunweijia The starting length of the second character is 3 String , Replace with aaaaaaaa;
The second function insert('yunweijia', -1, 3, 'bbbbbbbb') Due to -1 String length exceeded , Directly returns the original string ;
The third function insert('yunweijia', 2, 99, 'ccccccccc') Due to replacement length 99, The original length of the string is exceeded , So start with the second character , Intercept all the following characters , And replace with the specified character ccccccccc.
Case conversion function
Capitalize to lowercase
lower(str) perhaps lcase(str) You can string str All alphabetic characters in are converted to lowercase letters .
mysql> select lower('YUNweiJIA'), lcase('YUNweiJIA');
+--------------------+--------------------+
| lower('YUNweiJIA') | lcase('YUNweiJIA') |
+--------------------+--------------------+
| yunweijia | yunweijia |
+--------------------+--------------------+
1 row in set (0.00 sec)
mysql>
Small to capital
upper(str) perhaps ucase(str) You can string str All alphabetic characters in are converted to uppercase letters .
mysql> select upper('yunWEIjia'), ucase('yunWEIjia');
+--------------------+--------------------+
| upper('yunWEIjia') | ucase('yunWEIjia') |
+--------------------+--------------------+
| YUNWEIJIA | YUNWEIJIA |
+--------------------+--------------------+
1 row in set (0.00 sec)
mysql>
Function to get a string of specified length LEFT(s,n) and RIGHT(s,n)
left(s,n) Return string s The leftmost one at the beginning n Characters .
mysql> select left('yunweijia', 5);
+----------------------+
| left('yunweijia', 5) |
+----------------------+
| yunwe |
+----------------------+
1 row in set (0.00 sec)
mysql>
right(s,n) Return string s The rightmost one at the beginning n Characters .
mysql> select right('yunweijia', 5);
+-----------------------+
| right('yunweijia', 5) |
+-----------------------+
| eijia |
+-----------------------+
1 row in set (0.00 sec)
mysql>
A function that fills a string LPAD(s1,len,s2) and RPAD(s1,len,s2)
lpad(s1,len,s2) Return string s1, On the left is the string s2 Fill in to len Character length . If s1 Is longer than len, The return value is shortened to len character .
mysql> select lpad('yunweijia', 4, '@#'), lpad('yunweijia', 20, '@#');
+----------------------------+-----------------------------+
| lpad('yunweijia', 4, '@#') | lpad('yunweijia', 20, '@#') |
+----------------------------+-----------------------------+
| yunw | @#@#@#@#@#@yunweijia |
+----------------------------+-----------------------------+
1 row in set (0.00 sec)
mysql>
Because of the string yunweijia Longer than 4, So not only does it not need to be filled , Instead, you need to intercept from the left 4 Character string .
RPAD(s1,len,s2) Return string s1, To the right of it is a string s2 Fill in until len Character length . If the string s1 Is longer than len, The return value is shortened to len Character length .
mysql> select rpad('yunweijia', 4, '@#'), rpad('yunweijia', 20, '@#');
+----------------------------+-----------------------------+
| rpad('yunweijia', 4, '@#') | rpad('yunweijia', 20, '@#') |
+----------------------------+-----------------------------+
| yunw | [email protected]#@#@#@#@#@ |
+----------------------------+-----------------------------+
1 row in set (0.00 sec)
mysql>
Because of the string yunweijia Longer than 4, So not only does it not need to be filled , Instead, you need to intercept from the left 4 Character string .
Function to delete spaces LTRIM(s)、RTRIM(s) and TRIM(s)
ltrim(s) Return string s, The space character on the left side of the string is deleted ;
mysql> select '( yunweijia )', concat('(',ltrim(' yunweijia '),')');
+-----------------+----------------------------------------+
| ( yunweijia ) | concat('(',ltrim(' yunweijia '),')') |
+-----------------+----------------------------------------+
| ( yunweijia ) | (yunweijia ) |
+-----------------+----------------------------------------+
1 row in set (0.00 sec)
mysql>
rtrim(s) Return string s, The space character on the right side of the string is deleted ;
mysql> select '( yunweijia )', concat( '(', rtrim(' yunweijia '), ')' );
+-----------------+--------------------------------------------+
| ( yunweijia ) | concat( '(', rtrim(' yunweijia '), ')' ) |
+-----------------+--------------------------------------------+
| ( yunweijia ) | ( yunweijia) |
+-----------------+--------------------------------------------+
1 row in set (0.00 sec)
mysql>
trim(s) Delete string s Space on both sides ;
mysql> select '( yunweijia )', concat( '(', trim(' yunweijia '), ')' );
+-----------------+-------------------------------------------+
| ( yunweijia ) | concat( '(', trim(' yunweijia '), ')' ) |
+-----------------+-------------------------------------------+
| ( yunweijia ) | (yunweijia) |
+-----------------+-------------------------------------------+
1 row in set (0.00 sec)
mysql>
Delete the function of the specified string TRIM(s1 FROM s)
trim(s1 from s) Delete string s All substrings at both ends of the s1.s1 For optional , In an unspecified case , Delete the blank space .
mysql> select trim('@#' from '@#@#[email protected]#[email protected]#@#');
+---------------------------------------+
| trim('@#' from '@#@#[email protected]#[email protected]#@#') |
+---------------------------------------+
| [email protected]#jia |
+---------------------------------------+
1 row in set (0.00 sec)
mysql>
You can see , Delete only the contents on both sides of the specified string , The contents in the middle will not be deleted .
Repeat the function of generating string REPEAT(s.n)
repeat(s,n) Returns a string consisting of a repeating string s Composed string , character string s The number of is equal to n;
If n<=0, Then return an empty string ;
If s perhaps n by null, Then return to null.
mysql> select repeat('x', 10), repeat('x', 0), repeat('x', null);
+-----------------+----------------+-------------------+
| repeat('x', 10) | repeat('x', 0) | repeat('x', null) |
+-----------------+----------------+-------------------+
| xxxxxxxxxx | | NULL |
+-----------------+----------------+-------------------+
1 row in set (0.00 sec)
mysql>
Space function SPACE(n)
space(n) Returns a n String of spaces .
mysql> select concat('(', space(10), ')');
+-----------------------------+
| concat('(', space(10), ')') |
+-----------------------------+
| ( ) |
+-----------------------------+
1 row in set (0.00 sec)
mysql>
Substitution function REPLACE(s,s1,s2)
replace(s,s1,s2) Use string s2 Alternative string s All strings in s1;
mysql> select replace('yunweijia', 'i', '@#');
+---------------------------------+
| replace('yunweijia', 'i', '@#') |
+---------------------------------+
| [email protected]#[email protected]#a |
+---------------------------------+
1 row in set (0.00 sec)
mysql>
A function that compares the size of a string strcmp(s1,s2)
strcmp(s1,s2): If all strings are the same , Then return to 0;
According to the current classification order , The first parameter is less than the second , Then return to -1;
Other situations return to 1.
mysql> select strcmp('txt', 'txt'), strcmp('txt', 'txt2'), strcmp('txt2', 'txt');
+----------------------+-----------------------+-----------------------+
| strcmp('txt', 'txt') | strcmp('txt', 'txt2') | strcmp('txt2', 'txt') |
+----------------------+-----------------------+-----------------------+
| 0 | -1 | 1 |
+----------------------+-----------------------+-----------------------+
1 row in set (0.00 sec)
mysql>
Get substring function SUBSTRING(s,b,len) and MID(s,n,len)
substring(s,n,len) with len Parameter format , From a string s Returns a length and len Substring with the same character , Starting from position n.
Maybe it's right n Use a negative value . If so , The position of the substring starts at the end of the string n character , Countdown n Characters , Not the beginning of the string .
mysql> select substring('yunweijia', 5, 2) as coll, substring('yunweijia', -3) as coll1, substring('yunweijia', 3) as coss2;
+------+-------+---------+
| coll | coll1 | coss2 |
+------+-------+---------+
| ei | jia | nweijia |
+------+-------+---------+
1 row in set (0.00 sec)
mysql>
A function that matches the start of a substring
locate(str1,str)、position(str1 in str) and instr(str, str11) The functions of the three functions are the same , Returns a substring str1 In string str Start position in .
mysql> select locate('wei', 'yunweijia'), position('wei' in 'yunweijia'), instr('yunweijia', 'wei');
+----------------------------+--------------------------------+---------------------------+
| locate('wei', 'yunweijia') | position('wei' in 'yunweijia') | instr('yunweijia', 'wei') |
+----------------------------+--------------------------------+---------------------------+
| 4 | 4 | 4 |
+----------------------------+--------------------------------+---------------------------+
1 row in set (0.00 sec)
mysql>
Functions in reverse order of strings REVERSE(s)
reverse(s) The string s reverse , The order of the returned strings and s The strings are in reverse order .
mysql> select reverse('yunweijia') as coll, reverse('123456789') as coll1;
+-----------+-----------+
| coll | coll1 |
+-----------+-----------+
| aijiewnuy | 987654321 |
+-----------+-----------+
1 row in set (0.00 sec)
mysql>
A function that returns a string at a specified position
elt(n, str1, str2,...,strn): If n=1, Then the return value is the first string str1;
If n=2, Then the return value is the second string str2; And so on .
If n<1 Or greater than the number of parameters , The return value is null.
mysql> select elt(1, 'ni1', 'wo1', 'ta') as coll, elt(3, 'ni1', 'wo1', 'ta') as coll_1, elt(0, 'ni1', 'wo1', 'ta') as coll_2, elt(4, 'ni1', 'wo1', 'ta') as coll_3;
+------+--------+--------+--------+
| coll | coll_1 | coll_2 | coll_3 |
+------+--------+--------+--------+
| ni1 | ta | NULL | NULL |
+------+--------+--------+--------+
1 row in set (0.00 sec)
mysql>
Function that returns the specified string position FIELD(s,s1,s2,…,sn)
field(s,s1,s2,...,sn) Return string s In the list s1,s2,...,sn The first place in , If you can't find it s, Then return directly 0.
If s by null, Yes when returning 0.
mysql> select field('w', 'yun', 'yunwei', 'w', 'yunweijia') as coll, field('x', 'yun', 'yunwei', 'w', 'yunweijia') as coll_1, field(null, 'yun', 'yunwei', 'w', 'yunweijia') as coll_2;
+------+--------+--------+
| coll | coll_1 | coll_2 |
+------+--------+--------+
| 3 | 0 | 0 |
+------+--------+--------+
1 row in set (0.00 sec)
mysql>
Function that returns the substring position FIND_IN_SET(s1,s2)
find_in_set(s1,s2) Return string s1 In the string list s2 Where in , A string list is a list of strings separated by commas .
If s1 be not in s2 or s2 Is an empty string , The return value is 0;
If any of the parameters is null, The return value is null.
mysql> select find_in_set('X', 'yun,xwei,Xwei,X,jia');
+-----------------------------------------+
| find_in_set('X', 'yun,xwei,Xwei,X,jia') |
+-----------------------------------------+
| 4 |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql>
Function to select string MAKE_SET(x,s1,s2,…,sn)
make_set(x,s1,s2,...,sn) Function press x The binary number of from s1,s2,...,sn Select the string in the .
mysql> select make_set(5, 'a', 'b', 'c', 'd') as coll, make_set(1|4, 'a', 'b', 'c', 'd') as coll_1, make_set(1|4, 'a', 'b', null, 'c', 'd') as coll_2, make_set(0, 'a', 'b', 'c', 'd') as coll_3;
+------+--------+--------+--------+
| coll | coll_1 | coll_2 | coll_3 |
+------+--------+--------+--------+
| a,c | a,c | a | |
+------+--------+--------+--------+
1 row in set (0.00 sec)
mysql>
5 The binary of is 0101, From the right to the left 1 Position and number 3 Is it 1, So choose a、c;
1 The binary of is 1,4 The binary of is 100, The binary after the operation is 101, From the right to the left 1 Position and number 3 Is it 1, So choose a、c;
because null Do not participate in calculation , So it will not be added to the result , therefore coll_2 Only a.
thus , In this paper, the end .
For more information, go to VX official account “ Operation and maintenance home ” , Get the latest article .
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
------ “ Operation and maintenance home ” ------
linux Interrupt exception ,linuxuml modeling ,linux Compile multiple source files ,linux How to update the time ,linux Paging video tutorial in ,
linux How to decompress gzip,linux Unzip a directory , How to close linux gateway ,linux Document structure diagram ,linux Improve the permission to execute ,
linux What mark is used in the document , Network security linux strengthening ,linux Check email in ,linux Change the background color ,linux Build a website under the environment .
边栏推荐
- Wacom firmware update error 123, digital board driver cannot be updated
- 【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
- 云呐|怎样管理固定资产?如何进行固定资产管理?
- Qt文档阅读笔记-Audio Example解析
- It is suggested that Siyuan notes can be compatible with third-party sync disks
- cmake笔记
- Authing 支持 Zadig 啦!云原生用户统一认证快速对接
- Is it risky and safe to open a mobile stock account?
- JS calendar style pie chart statistics plug-in
- Official wechat product! Applet automation framework minium sharing Preview
猜你喜欢

OKRK3399開發板預留I2C4掛載EEPROM

OpenCV计算外包矩形

固定资产管理系统哪家好?固定资产管理平台有哪些?

【测试平台开发】二十、完成编辑页发送接口请求功能

Spotlight light box JS plug-in full screen enlarged picture

Sword finger offer 46. translate numbers into strings
It is suggested that Siyuan notes can be compatible with third-party sync disks

【数组&&字符串&&宏练习题】

直播课堂系统03补充-model类及实体
![[can I do your first project?] Detailed introduction and Simulation Implementation of gzip](/img/92/dfef5887e2313f8025baf11c8f4379.png)
[can I do your first project?] Detailed introduction and Simulation Implementation of gzip
随机推荐
First acquaintance and search set
右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting
C language introduction practice (11): enter a group of positive integers and sum the numbers in reverse order
Qu'est - ce que le codage par titre?
R language practical application case: drawing part (III) - drawing of multiple combination patterns
websocket通用化封装设计与实现
生成订单号
Authing 支持 Zadig 啦!云原生用户统一认证快速对接
运维高级作业02
About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion
Towhee weekly model
win11安装系统提示virtualBox不兼容需要卸载virtual的解决办法,但是卸载列表找不到virtual的解决办法
Uni app knowledge points and records of problems and solutions encountered in the project
Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?
Some libraries that can perform 2D or 3D triangulation
【数组&&字符串&&宏练习题】
The win11 installation system prompts that VirtualBox is incompatible and needs to uninstall the solution of virtual, but the uninstall list cannot find the solution of virtual
Solve a series of problems in using Bert encoder
Work notes: one time bag grabbing