[MYSQL Quick Start] Common Functions: Text Functions

Example table department:

Common text processing functions:

function illustrate
left Returns the character to the left of the string
length Returns the length of the string
lower

convert string to lowercase

upper convert string to uppercase
ltrim Remove spaces from the left of the string
rtrim Remove spaces from the right of the string
substring returns a substring of the string
substring_index Delimited string with delimiter
locate find a substring of a string

concat(s1,s2,..)

connection string

Verify the use of lower, upper, right, left, length, locate functions

select dept_no,dept_name,upper(dept_name) as '转为大写',
lower(dept_name) as'转为小写',
left(dept_name,4) as'左边4个字符',
right(dept_name,4) as'右边4个字符',
length(dept_name) as'字符串的长度',
locate('an',dept_name) as'文本an出现的位置'
from departments;

 

 locate('a', 'b'): the first occurrence of substring a in string b

concat('a','b','c'): connect abc and return the string generated after the connection. Returns null if either parameter is null.

substring('s',startindex,length): intercepts the string, the first parameter is the string to be intercepted, the second parameter is the position to start intercepting, and the third parameter is the length of the interception. If the length of the interception is empty, it means that the whole is intercepted.

select substring('abcdefg',5);从第5个位置开始截取
->efg
select substring('abcdefg',5,2);从第5个位置开始截取长度为2的子串
->ef
select substring('abcdefg',-3);位置为负数时,代表从右边往左数,-3代表右数三位,一直到最后
->efg
select substring('abcdefg',-3,2);从右往左数3位截取长度为2的子串
->ef

substring_index('s', 'separator', number): the number is positive, and there are several from left to right; the number is negative, and the number is from right to left

example:

Statistics by gender:

 

select substring(profile,',',-1)gender,count(*)number
from user_submit
group by gender;

Cut out age: 

 

select 
substring_index(substring_index(profile,',',3),',',-1) as age,
count(device_id) as number
from user_submit
group by age;

Extract the username from the url: 

 

select device_id,substring_index(blog_url,"/",-1)
as user_name
from user_submit;

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124277591
Recommended