Table of contents
1.concat: combine two or more strings into one string
2.length and char_length functions: get the length of the string in bytes and characters
3.left: returns the left part of the string with the specified length
4.right: return the right part of the string with the specified length
5.replace: Search and replace substrings in strings.
6.substring: Extract a substring from a position with a specific length.
7.trim: Remove unwanted characters from a string.
8. find_in_set: Find a string in a comma-separated list of strings
9.format: Format numbers with specific regional settings, rounded to decimal places.
1.concat: combine two or more strings into one string
MySQL CONCAT()
functions take one or more string arguments and concatenate them into a single string. CONCAT()
Function requires at least one argument, otherwise an error is raised
Convert all arguments to string types before concatenating. If any argument is NULL
, CONCAT()
the function returns NULL
a value.
If you want to use connectors, you need to splice connectors between the connected items.
查询语句:select CONCAT(supplier_id,'-',supplier_name) from bms_bills_memo where id = 4
结果:6-供应商4
CONCAT_WS()
Function concatenates two or more string values with predefined delimiters.
The first parameter is the delimiter, followed by the parameters to be spliced
Returns null only if the delimiter is null, if the item is null, no concatenation
查询语句:
select CONCAT_WS('-',supplier_id,supplier_name) from bms_bills_memo where id = 4
select CONCAT_WS('-',null,supplier_name) from bms_bills_memo where id = 4
select CONCAT_WS('-',null,null) from bms_bills_memo where id = 4
结果:
6-供应商4
供应商4
空
2.length and char_length functions: get the length of the string in bytes and characters
length: Get the length of the string in bytes
char_length: Get the length of the string in characters
Byte is a unit of measurement, which indicates the amount of data. It is a unit of measurement used by computer information technology to measure storage capacity. Usually, one byte is equal to eight bits.
Character (Character) Letters, numbers, words and symbols used in computers, such as 'A', 'B', '$', '&', etc.
Generally, in the English state, a letter or character occupies one byte, and a Chinese character is represented by two bytes.
- In ASCII code, an English letter (not case sensitive) is one byte, and a Chinese character is two bytes.
- In UTF-8 encoding, an English character is one byte, and a Chinese character is three bytes.
- In Unicode encoding, one English is one byte, and one Chinese is two bytes.
- Symbol: English punctuation is one byte, Chinese punctuation is two bytes. For example: English period . occupies 1 byte, Chinese period . Occupies 2 bytes in size.
- In UTF-16 encoding, the storage of an English alphabet character or a Chinese character requires 2 bytes (the storage of some Chinese characters in the Unicode extension area requires 4 bytes).
- In UTF-32 encoding, the storage of any character in the world requires 4 bytes.
查询语句:
select LENGTH(supplier_name) from bms_bills_memo where id = 4
select CHAR_LENGTH(supplier_name) from bms_bills_memo where id = 4
结果:
10
4
表中数据
supplier_name:供应商4
In conjunction with
SELECT postid,
title,
IF(CHAR_LENGTH(excerpt) > 20,
CONCAT(LEFT(excerpt,20), '...'),
excerpt) summary
FROM posts;
We use IF
a function to check if the length of the excerpt column is greater than 20
, and use the CONCAT statement to concatenate excerpt
the value of the column with an ellipsis ( ), otherwise just get the entire excerpt ( ) content...
excerpt
3.left: returns the left part of the string with the specified length
查询语句:
select LEFT(client_name,4) from bms_bills_memo where id = 4
结果:
青岛自贸
表中数据
client_name:青岛自贸新零售体验中心有限公司
LEFT()
The function accepts two parameters:
str
is the string from which to extract the substring.length
is a positive integer specifying the number of characters to return from the left.
LEFT()
The function returns str
the leftmost length characters in a string. Returns the value if str
the or length
argument is .NULL
NULL
If length
true 0
or negative, LEFT
the function returns an empty string. If length
greater than str
the length of the string, LEFT
the function returns the entire str
string.
4.right: return the right part of the string with the specified length
查询语句:
select RIGHT(client_name,4) from bms_bills_memo where id = 4
结果:
有限公司
表中数据:
client_name:青岛自贸新零售体验中心有限公司
5.replace: Search and replace substrings in strings.
Takes three arguments, it replaces the string string
inold_string
new_string
Note that when searching for text to replace, MySQL uses case-sensitive matching to perform the search for the string to replace
does not support regular expressions
查询语句:
select REPLACE(client_name,'青岛','威海') from bms_bills_memo where id = 4
结果:
威海自贸新零售体验中心有限公司
查询语句:
select REPLACE(bill_of_lading_no,'td','ab') from bms_bills_memo where id = 4
结果:
TD10004
结果没有发生变化,因为区分大小写
6.substring: Extract a substring from a position with a specific length.
SUBSTRING(string,position);
SUBSTRING(string FROM position);
There are two parameters:
string
The argument is the string whose substring is to be extracted.position
The parameter is an integer specifying the starting character of the substring, whichposition
can be a positive or negative integer.
If position
positive, SUBSTRING
the function extracts the substring from the beginning of the string.
If position
the argument is zero, SUBSTRING
the function returns an empty string.
Functions can be called using the SQL standard syntax with FROM
the keywordSUBSTRING
查询语句:
select SUBSTRING(bill_of_lading_no,2) from bms_bills_memo where id = 4
结果:
D10004
查询语句:
select SUBSTRING(bill_of_lading_no,-2) from bms_bills_memo where id = 4
结果:
04
表中数据:
bill_of_lading_no:TD10004
If you want to specify the length of the substring to be extracted from a string, you can use SUBSTRING
a function of the form
SUBSTRING(string,position,length);
SUBSTRING(string FROM position FOR length);
查询语句:
select SUBSTRING(bill_of_lading_no,2,5) from bms_bills_memo where id = 4
结果:
D1000
查询语句:
select SUBSTRING(bill_of_lading_no,-7,5) from bms_bills_memo where id = 4
结果:
TD100
表中数据:bill_of_lading_no:TD10004
SUBSTR()
function is SUBSTRING()
a synonym for function, so they can be used interchangeably.
Usage of SUBSTRING_INDEX
Split according to a specific symbol and take out the corresponding value
查询语句:
select SUBSTRING_INDEX('qwe-etr-tyu','-',1)
select SUBSTRING_INDEX('qwe-etr-tyu','-',2)
select SUBSTRING_INDEX('qwe-etr-tyu','-',-1)
select SUBSTRING_INDEX('qwe-etr-tyu','-',-2)
结果:
qwe
qwe-etr
tyu
etr-tyu
7.trim: Remove unwanted characters from a string.
TRIM([{BOTH|LEADING|TRAILING} [removed_str]] FROM str);
You can use the LEADING
, TRAILING
or BOTH
option to explicitly instruct TRIM()
the function to remove leading, trailing, or both leading and trailing unnecessary characters from the string.
If you don't specify anything, TRIM()
the function defaults to using BOTH
options.
[removed_str]
is the string to delete. By default it is a space. This means that if you do not specify a specific string, TRIM()
the function only removes spaces.
str
is removed_str
the string to remove subcharacters from.
TRIM()
The function returns a string with unwanted characters removed.
查询语句:
//从字符串中除去前导和尾随空格
SELECT TRIM(' MySQL TRIM Function ');
//仅删除前导空格
SELECT TRIM(LEADING FROM ' MySQL TRIM Function ');
//仅删除尾随空格
SELECT TRIM(TRAILING FROM ' MySQL TRIM Function ');
//删除字符串末尾的换行符
-- 方式一
SELECT
TRIM(TRAILING '\n' FROM field_name)
FROM table_name;
-- 方式二
SELECT
TRIM(TRAILING '\r' FROM field_name)
FROM table_name;
-- 方式三
SELECT
TRIM(TRAILING '\r\n' FROM field_name)
FROM table_name;
If you want to remove only leading or trailing spaces, you can use other string functions: LTRIM
andRTRIM
Use LTRIM
a function to remove leading spaces from a string
SELECT LTRIM('MySQL LTRIM function');
Use RTRIM()
a function to remove trailing spaces from a string
SELECT RTRIM('MySQL RTRIM function ');
8. find_in_set: Find a string in a comma-separated list of strings
FIND_IN_SET(needle,haystack);
FIND_IN_SET()
The function accepts two parameters:
- The first parameter
needle
is the string to look for. - The second argument
haystack
is a comma-separated list of strings to search.
FIND_IN_SET()
The function returns an integer or a NULL
value depending on the value of the argument:
- If
needle
orhaystack
isNULL
, the function returnsNULL
a value. - Returns zero if
needle
nothaystack
in, or the empty string.haystack
- If
needle
inhaystack
, returns a positive integer.
Note that this function will not work properly if you needle
include a comma ( ). ,
Also, if needle
it is a constant string, and haystack
it is a SET
column of type , MySQL will use bit arithmetic optimization.
查询语句:
select FIND_IN_SET('red','yellow,red,blue')
结果:
2
where you can use find_in_set directly
SELECT
name, belts
FROM
divisions
WHERE
FIND_IN_SET('red', belts);
SELECT
name, belts
FROM
divisions
WHERE
NOT FIND_IN_SET('black', belts);
The FIND_IN_SET function has the same functionality as the IN operator
column IN (x, y, z)
The expression is FIND_IN_SET(column, 'x,y,z')
the same as .
IN
Operators can accept any number of arguments, each separated by a comma. However, FIND_IN_SET
the function has only two parameters.
IN
Operators are used when you want to match a value against a list of values in the database . And when you want to match a value against a comma-separated list of values stored in the database, you can use FIND_IN_SET
a function.
9.format: Format numbers with specific regional settings, rounded to decimal places.
FOMRAT(N,D,locale);
FORMAT
The function formats the number N to the format, eg "#,###,###.##"
, rounded to D
one decimal place. It returns a value as a string.
FORMAT
The function accepts three parameters:
N
is the number to format.D
is the number of decimal places to round off.locale
is an optional parameter that determines the thousand separator and the grouping between separators. Iflocale
the operator is omitted, MySQL will use it by defaulten_US
. The following link provides all locale names supported by MySQL: http://dev.mysql.com/doc/refman/5.7/en/locale-support.html
查询语句:
select format(123456.1258,2)
select format(123456.1248,2)
结果:
123,456.13
123,456.12
Two functions can be combined: FORMAT
and CONCAT
. FORMAT
The function rounds the inventory value format to 2
decimal places. And the concat function adds the RMB symbol ( ¥
) at the beginning of the inventory value string
SELECT
productname,
CONCAT('¥',
FORMAT(quantityInStock * buyPrice, 2)) stock_value
FROM
products;