手记

MySQL 一些重要函数(字符函数)

  • 字符连接函数

select concat('love','-','imooc');

love-imooc


select concat_ws('','love','imooc','mysql');

loveimoocmysql


  • FORMAT();数字格式化函数(千分位)

SELECT FORMAT(12345.12,2);
12,345.12


  • LOWER();字符小写

SELECT LOWER('MySQL');
mysql


  • UPPER();字符大写

SELECT UPPER('mysql');
MYSQL


  • LEFT();取左侧字符

SELECT LEFT('IMOOC',2);
IM


  • RIGHT();取右侧字符

SELECT RIGHT('IMOOC',2);
OC


  • LENGTH();获取字符长度(包括空格)

SELECT LENGTH('IMOOC');
5


  • LTRIM();删除前导空格

SELECT LENGTH(LTRIM(' IMOOC '));
8


  • RTRIM();删除后续空格

SELECT LENGTH(RTRIM('IMOOC '));
5


  • TRIM();删除空格

SELECT LENGTH(TRIM('I M O O C '));
5
SELECT TRIM(LEADING'?' FROM '??MYSQL???');--删除from后字符中前面的‘?’
MYSQL???
SELECT TRIM(TRAILING'?' FROM '??MYSQL???');--删除from后字符中后面的‘?’
??MYSQL
SELECT TRIM(BOTH'?' FROM '??MYSQL???');--删除from后字符中后面的‘?’
MYSQL
SELECT TRIM(BOTH'?' FROM '??MY?SQL???');--删除from后字符中前面和后面的‘?’
MY?SQL


  • REPLACE();替换字符函数

SELECT REPLACE('??MY?SQL???','?','');
MYSQL
SELECT REPLACE('??MY?SQL???','?','!');
!
!MY!SQL!!!*


  • SUBSTRING();截取字符串

SELECT SUBSTRING('IMOOC MYSQL',2,6);
MOOC M
SELECT SUBSTRING('IMOOC MYSQL',-2);
QL


10人推荐
随时随地看视频
慕课网APP