MySQL 字符串函数
函数名/说明 | 应用实例 |
---|---|
ASCII(s) 返回s字符串的第一个字符的 ASCII 码 | SELECT ASCII(‘Hello World’); - - 即返回 H字符的ASCII为:72 |
concat (s1,…,sn); 字符串连接函数; 任何参数为null时,返回 null | select concat (‘hello’,‘world’);- - 返回:helloworld select concat(‘hello’, ‘world’, null); - - 返回:NULL |
concat_ws (c, s1,…,sn); 指定c为s1~sn的连接符; c参为null时, 返回 null | select concat_ws (’-’, ‘hello’, ‘world’); - - 返回:hello-world select concat_ws (null, ‘hello’, ‘world’); - - 返回:NULL |
lower (s) 同 lcase (s)将s字符串转换为小写字母 | select lower (‘Hello World’);- - 返回: hello world |
LCASE(s) 将s字符串转换为小写字母 | select lcase (‘Hello World’);- - 返回: hello world |
upper (s) 同 ucase (s)将s字符串转换为大写字母 | select upper (‘Hello World’);- - 返回: HELLO WORLD |
length (s) 返回字符串的字节长度 | select length (‘hello world’);- - 返回:11 |
char_length(s) 返回字符串 s 的字符数 | select char_length(“MySQL”); - - 返回:5 |
character_length(s) 返回字符串 s 的字符数 | select character_length(“MySQL”); - - 返回:5 |
LPAD(s1,len,s2) 字符串s1的开始处填充 s2字符串,使字符串长度 len | SELECT LPAD(‘abc’, 5, ‘xx’); - - 将字符串xx填充到abc字符串:xxabc |
RPAD(s1,len,s2) s1字符串结尾处添加s2字符串,使字符串的长度 len | SELECT RPAD(‘abc’,5,‘xx’); - - 将字符串 xx 填充到 abc 字符串的结尾处:abcxx |
ltrim (s) 删除 字符串s左侧空格字符 | select ltrim (’ hello’); - - 可删除多个空格; 返回:hello |
rtrim (s)删除 字符串s右侧空格字符 | select rtrim ('hello '); - - 可删除多个空格; 返回:hello |
trim (s)删除 字符串s两侧空格字符 | select trim (’ hello world '); - - 内部空格不影响; 返回:hello world |
SPACE(n) 返回 n 个空格 | SELECT SPACE(10); - - 返回 10 个空格 |
SUBSTR(s, start, length) s字符串 start位置截取长度 length 的子字符串 | SELECT SUBSTR(“MySQL”, 2, 3); - - 返回: ySQ |
substring (s, n, len) 同 MID (s,n,len)s字符串,从n位置起截取 len长度的字符串; 若n为负数,则字符串尾开始计数; | select substring (‘hello world’, 1, 7); - - 返回:hello w select substring (‘hello world’, -4, 3); - - 返回:orl |
substring_index(s,dmt,num); 返回从s字符串的第num个出现的分隔符dmt后的子串: 若num是正数,返回第num个字符左边的字符串; 若num是负数,返回第num个字符右边的字符串 | select substring_index(‘ab’,’’,1); - - a select substring_index(‘ab’,’’,-1); - - b select substring_index(substring_index(‘abcde’,’’,3),’’,-1); - - 返回:c |
left (s, n) s字符串,截取左边n个字符 | select left (‘hello world’, 5); - - 返回:hello |
right (s, n) s字符串,截取右边n个字符 | select right (‘hello world’, 5); - - 返回:world |
replace (s, from_s, to_s) 把字符串s中的 from_s 替换为 to_s | select replace (‘hello world’, ‘world’, ‘sql’); - - 返回:hello sql |
repeat(s,n) 将字符串 s 重复 n 次 | select repeat(‘abc’,3); - - 将字符串 abc重复3次:abcabcabc |
insert(s1,x,len,s2) 字符串s2 替换 s1,位置x开始,长度为 len字符串 | SELECT INSERT(“google”, 1, 6, “mysql”); - - 返回:mysqle |
REVERSE(s) 将s字符串 反序 | SELECT REVERSE(‘abc’); - - 将字符串 abc 的顺序反过来 |
format (x, n) 以四舍五入格式化 x,小数点n位; 若n=0 则返回结果不含小数 | select format (34.567, 2); - - 返回字符:34.57 select format (34.5, 2); - - 返回字符:34.50 select format (34.567, 0); - - 返回字符:35 |
FIELD(s,s1,s2…) 返回第1个s字符串,字符串列表(s1,s2…)的位置 | SELECT FIELD(‘c’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’); - - 返回字符串 c 在列表值中的位置:3 |
FIND_IN_SET(s1,s2) 返回s2字符串中,字符串s1匹配的位置 | SELECT FIND_IN_SET(‘c’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’); - - 返回字符串 c 在指定字符串中的位置 |
LOCATE(s1,s) 从字符串 s 中获取 s1 的开始位置 | SELECT LOCATE(‘st’,‘myteststring’); - - 获取st在字符串myteststring 的位置:5 SELECT LOCATE(‘b’, ‘abc’); - - 返回字符串 abc 中 b 的位置:2 |
POSITION(s1 IN s) 从字符串 s 中获取 s1 的开始位置 | SELECT POSITION(‘b’ in ‘abc’); - - 返回字符串 abc 中 b 的位置:2 |
STRCMP(s1,s2); 比较函数: 若 s1= s2 返回 0; 若 s1>s2 返回 1; 若 s1<s2 返回 -1 | SELECT STRCMP(“hello”, “world”); - - 返回:-1 |