MySQL 内置函数
程序员文章站
2022-03-09 13:54:37
CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符。一个多字节字符算作一个单字符。 CONCAT(str1,str2,...) 字符串拼接, 如有任何一个参数为NULL ,则返回值为 NULL。 CONCAT_WS(separator,str1,str2,...) 字符 ......
char_length(str)
返回值为字符串str 的长度,长度的单位为字符。一个多字节字符算作一个单字符。
+------------------------+ | char_length('klvchen') | +------------------------+ | 7 | +------------------------+ 1 row in set (0.00 sec) mysql> select char_length('你'); +--------------------+ | char_length('你') | +--------------------+ | 1 | +--------------------+ 1 row in set (0.00 sec)
concat(str1,str2,...)
字符串拼接, 如有任何一个参数为null ,则返回值为 null。
mysql> select concat('kl','v','chen'); +-------------------------+ | concat('kl','v','chen') | +-------------------------+ | klvchen | +-------------------------+ 1 row in set (0.00 sec)
concat_ws(separator,str1,str2,...)
字符串拼接(自定义连接符)concat_ws()不会忽略任何空字符串。 (然而会忽略所有的 null)
mysql> select concat_ws('_','chen','wj'); +----------------------------+ | concat_ws('_','chen','wj') | +----------------------------+ | chen_wj | +----------------------------+ 1 row in set (0.01 sec) mysql> select concat_ws('_','chen',' wj ', null, 'hehe'); +--------------------------------------------+ | concat_ws('_','chen',' wj ', null, 'hehe') | +--------------------------------------------+ | chen_ wj _hehe | +--------------------------------------------+ 1 row in set (0.00 sec)
conv(n,from_base,to_base)
进制转换
mysql> select conv('a',16,2); +----------------+ | conv('a',16,2) | +----------------+ | 1010 | +----------------+ 1 row in set (0.00 sec)
format(x,d)
将数字x 的格式写为'#,###,###.##',以四舍五入的方式保留小数点后 d 位, 并将结果以字符串的形式返回。若 d 为 0, 则返回结果不带有小数点,或不含小数部分。
mysql> select format(1.23,4); +----------------+ | format(1.23,4) | +----------------+ | 1.2300 | +----------------+ 1 row in set (0.00 sec)
insert(str,pos,len,newstr)
str 原字符串; pos 要替换位置其实位置(从1开始); len:替换的长度;newstr:需要替换的符串
简洁概况为:先从 pos 处删除 len 长度的字符串,再由新的字符串代替
mysql> select insert('klvchen', 1, 1, 'j'); +------------------------------+ | insert('klvchen', 1, 1, 'j') | +------------------------------+ | jlvchen | +------------------------------+ 1 row in set (0.00 sec) mysql> select insert('klvchen', 1, 1, 'ja'); +-------------------------------+ | insert('klvchen', 1, 1, 'ja') | +-------------------------------+ | jalvchen | +-------------------------------+ 1 row in set (0.00 sec) mysql> select insert('klvchen', 1, 2, 'a'); +------------------------------+ | insert('klvchen', 1, 2, 'a') | +------------------------------+ | avchen | +------------------------------+ 1 row in set (0.00 sec)
instr(str,substr)
返回字符串 str 中子字符串的第一个出现位置。
mysql> select instr('klvchen','l'); +----------------------+ | instr('klvchen','l') | +----------------------+ | 2 | +----------------------+ 1 row in set (0.00 sec)
locate(substr,str,pos)
获取子序列索引位置
mysql> select locate('l','klvchen',1); +-------------------------+ | locate('l','klvchen',1) | +-------------------------+ | 2 | +-------------------------+ 1 row in set (0.01 sec)
left(str,len)
返回字符串 str 从开始的 len 位置的子序列字符。
mysql> select left('klvchen',2); +-------------------+ | left('klvchen',2) | +-------------------+ | kl | +-------------------+ 1 row in set (0.00 sec)
right(str,len)
从字符串 str 开始,返回从后边开始 len个字符组成的子序列
mysql> select right('klvchen', 2); +---------------------+ | right('klvchen', 2) | +---------------------+ | en | +---------------------+ 1 row in set (0.00 sec)
lower(str)
字符串转化为小写
mysql> select lower('klvchen'); +------------------+ | lower('klvchen') | +------------------+ | klvchen | +------------------+ 1 row in set (0.00 sec)
upper(str)
字符串转化为大写
mysql> select upper('klvchen'); +------------------+ | upper('klvchen') | +------------------+ | klvchen | +------------------+ 1 row in set (0.00 sec)
ltrim(str)
返回字符串 str ,左边的空格字符被删除。
mysql> select ltrim(' klvchen'); +--------------------+ | ltrim(' klvchen') | +--------------------+ | klvchen | +--------------------+ 1 row in set (0.00 sec)
rtrim(str)
返回字符串 str ,结尾空格字符被被删除。
mysql> select rtrim('klvchen '); +----------------------+ | rtrim('klvchen ') | +----------------------+ | klvchen | +----------------------+ 1 row in set (0.00 sec)
trim([{both | leading | trailing} [remstr] from] str) trim(remstr from] str)
返回字符串 str , 其中所有remstr 前缀和/或后缀都已被删除。若分类符both、leadin或trailing中没有一个是给定的,则假设为both 。 remstr 为可选项,在未指定情况下,可删除空格。
mysql> select trim(' kl '); +------------------+ | trim(' kl ') | +------------------+ | kl | +------------------+ 1 row in set (0.00 sec) mysql> select trim(leading 'x' from 'xxklxx'); +----------------------------------+ | trim(leading 'x' from 'xxklxx') | +----------------------------------+ | klxx | +----------------------------------+ 1 row in set (0.00 sec) mysql> select trim(both 'x' from 'xxklxx'); +-------------------------------+ | trim(both 'x' from 'xxklxx') | +-------------------------------+ | kl | +-------------------------------+ 1 row in set (0.00 sec) mysql> select trim(trailing 'x' from 'xxklxx'); +-----------------------------------+ | trim(trailing 'x' from 'xxklxx') | +-----------------------------------+ | xxkl | +-----------------------------------+ 1 row in set (0.00 sec)
repeat(str,count)
返回一个由重复的字符串 str 组成的字符串,字符串str重复的次数等于count的值 。
若 count <= 0,则返回一个空字符串;若str 或 count 为 null,则返回 null 。
mysql> select repeat('ha',3); +----------------+ | repeat('ha',3) | +----------------+ | hahaha | +----------------+ 1 row in set (0.00 sec)
replace(str,from_str,to_str)
返回字符串str 以及所有被字符串to_str替代的字符串from_str 。
mysql> select replace('klvchel', 'l', 'i'); +------------------------------+ | replace('klvchel', 'l', 'i') | +------------------------------+ | kivchei | +------------------------------+ 1 row in set (0.00 sec)
reverse(str)
返回字符串 str ,顺序和字符顺序相反。
mysql> select reverse('klvchen'); +--------------------+ | reverse('klvchen') | +--------------------+ | nehcvlk | +--------------------+ 1 row in set (0.00 sec)
substring(str,pos,len)
获取字符串子序列
mysql> select substring('klvchen', 1, 3); +----------------------------+ | substring('klvchen', 1, 3) | +----------------------------+ | klv | +----------------------------+ 1 row in set (0.00 sec) mysql> select substring('klvchen', 3); +-------------------------+ | substring('klvchen', 3) | +-------------------------+ | vchen | +-------------------------+ 1 row in set (0.00 sec) mysql> select substring('klvchen' from 3); +-----------------------------+ | substring('klvchen' from 3) | +-----------------------------+ | vchen | +-----------------------------+ 1 row in set (0.00 sec) mysql> select substring('klvchen', -3); +--------------------------+ | substring('klvchen', -3) | +--------------------------+ | hen | +--------------------------+ 1 row in set (0.00 sec) mysql> select substring('klvchen', -3, 2); +-----------------------------+ | substring('klvchen', -3, 2) | +-----------------------------+ | he | +-----------------------------+ 1 row in set (0.00 sec) mysql> select substring('klvchen' from -3 for 2); +------------------------------------+ | substring('klvchen' from -3 for 2) | +------------------------------------+ | he | +------------------------------------+ 1 row in set (0.01 sec)