当前位置:网站首页>MySQL - common functions - string functions
MySQL - common functions - string functions
2022-07-07 02:38:00 【Sauerkraut】
function
MySQL The function will process the parameters passed in , And return a processing result , That is, return a value .
MySQL Contains a large number and rich functions , Let's talk about dozens of commonly used , For the remaining rare functions, we can go to 「MySQL Reference manual 」 Inquire about .
String function
| function | effect |
| UPPER( Column | character string ) | Convert each character of the string to uppercase |
| LOWER( Column | character string ) | Convert each character of the string to lowercase |
| CONCAT(str1,str2,. . . ) | Connect all strings into one string |
| REPLACE( Column | character string , Old string , New string ) | Replace the old string with the new string |
| LENGTH( Column | character string ) | Find the string length |
| SUBSTR( Column | character string , Starting point [, length ]) | String interception |
| LEFT(str,len) | Get the left side of the string len A string of characters |
| RIGHT(str,len) | Get the right side of the string len A string of characters |
| MID(str,pos,len) | Get the string from pos( Number one ) Position start , The length is len String |
| ASCII( character ) | Returns the decimal integer corresponding to the specified character |
| CHR( Numbers ) | Returns the character corresponding to an integer |
RPAD( Column | character string , length , Fill character ) LPAD( Column | character string , length , Fill character ) | Fill the right or left of the string with the specified character |
| LTRIM( character string )、RTRIM( character string ) | Remove the left or right space of the string |
| TRIM( Column | character string ) | Remove the space around the string |
| INSTR( Column | character string , String to find , Starting position , Location of occurrence ) | Find out whether a substring appears at the specified position |
| UPPER( Column | character string ) | Convert each character of the string to uppercase |

If you use emp Table shows that the test data is output many times , because emp There are many pieces of data in the table , Every piece of data should be executed

Use MySQL Virtual tables in , There's only one line of data , It can be used to test functions , Or not
You can find : If case is combined , Use LOWER All of them become lowercase 
| CONCAT(str1,str2,. . . ) | Connect all strings into one string |
Single and double quotation marks can be used for strings
If there are quotation marks in the string ' ' ', The escape character is required ' \ ' Transference

| REPLACE( Column | character string , Old string , New string ) | Replace the old string with the new string |

| LENGTH( Column | character string ) | Find the string length |

Get the left side of the string 2 A string of characters 、 Get the right side of the string 2 A string of characters 、 Get the middle of the string 2 A string of characters
| LEFT(str,len) | Get the left side of the string len A string of characters |
| RIGHT(str,len) | Get the right side of the string len A string of characters |
| MID(str,pos,len) | Get the string from pos( Number one ) Position start , The length is len String |
SELECT LEFT('hello',2),RIGHT('hello',2),MID('case',2,2);
| ASCII( character ) | Returns the decimal integer corresponding to the specified character |
| CHR( Numbers ) | Returns the character corresponding to an integer |
SELECT ASCII('A');
SELECT CHAR(65);
RPAD( Column | character string , length , Fill character ) LPAD( Column | character string , length , Fill character ) | Fill the right or left of the string with the specified character |
SELECT RPAD('Hello',10,'*'),LPAD('Hello',10,'*');

Fill the left and right sides 5 individual ' * ', Fill the right first , Treat the whole as a string , Fill in the front ' * '
SELECT LPAD(RPAD('Hello',10,'*'),15,'*');
| LTRIM( character string )、RTRIM( character string ) | Remove the left or right space of the string |
| TRIM( Column | character string ) | Remove the space around the string |
SELECT LTRIM(' 123 *') Left ,RTRIM('* 123 ') Right ,TRIM(' 123 ') AS 'All';
You can't remove the space in the middle of the string

| INSTR( Column | character string , String to find , Starting position , Location of occurrence ) | Find out whether a substring appears at the specified position |
SELECT INSTR(' I love C Language ','C Language ');
Not found

1. Capitalize the employee's name Smith
SELECT UPPER(LEFT(ename,1)) FROM emp;
SELECT UPPER(LEFT(ename,1)),LOWER(SUBSTR(ename,2)) FROM emp;
SELECT CONCAT(UPPER(LEFT(ename,1)),LOWER(SUBSTR(ename,2))) FROM emp;
2. The length of the name is 5 All employee information for Or use fuzzy queries LIKE MySQL --- Database query - Conditions of the query _ Xiaoxuecai's blog -CSDN Blog
SELECT * FROM emp WHERE LENGTH(ename)=5;

3. The first three letters of the employee's name are 'JAM' The employees' , Intercept start point and length
SELECT * FROM emp WHERE SUBSTR(ename,1,3)='JAM';
4. Query all employee information , But the first three letters of the name are not displayed
| SUBSTR( Column | character string , Starting point [, length ]) | String interception |
SELECT ename Original string ,SUBSTR(ename,3) AS ' Substring ' FROM emp;
Query all employee information , But only the last three letters of the name are displayed
| SUBSTR( Column | character string , Starting point [, length ]) | String interception |
SELECT ename Original string ,SUBSTR(ename,-3) AS ' Substring ' FROM emp;
边栏推荐
- 基于ensp防火墙双击热备二层网络规划与设计
- Detailed explanation of line segment tree (including tested code implementation)
- Introduction to FLIR blackfly s industrial camera
- 记一次JAP查询导致OOM的问题分析
- Rethinking of investment
- unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
- [unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
- Safety delivery engineer
- 测试优惠券要怎么写测试用例?
- Argo workflows source code analysis
猜你喜欢

How to build a 32core raspberry pie cluster from 0 to 1

豆瓣平均 9.x,分布式领域的 5 本神书!

Lumion 11.0 software installation package download and installation tutorial

postgresql之integerset

6-6 vulnerability exploitation SSH security defense

Compress JS code with terser

Draco - gltf model compression tool

MATLB|具有储能的经济调度及机会约束和鲁棒优化

MES管理系统的应用和好处有哪些

1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
随机推荐
6-6 vulnerability exploitation SSH security defense
压缩 js 代码就用 terser
MES管理系统的应用和好处有哪些
QPushButton-》函数精解
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
企业中台建设新路径——低代码平台
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
4 -- Xintang nuc980 mount initramfs NFS file system
Use of fiddler
牛客编程题--必刷101之双指针篇
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
软件测试——Jmeter接口测试之常用断言
豆瓣平均 9.x,分布式领域的 5 本神书!
3D laser slam: time synchronization of livox lidar hardware
Mmdetection3d loads millimeter wave radar data
Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
Hash table and full comments
The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
Leetcode:minimum_ depth_ of_ binary_ Tree solutions