1、字符长度计算
-- 字符串位长度(一字节8位) select BIT_LENGTH('Hello') a1,BIT_LENGTH('张三') a2; -- 字符串个数 select CHAR_LENGTH('Hello') a1,CHAR_LENGTH('张三') a2; -- 字符串 字节数 select LENGTH('Hello') a1,LENGTH('张三') a2; select OCTET_LENGTH('Hello') a1,OCTET_LENGTH('张三') a2;
2、base64加解密函数
-- 貌似中文会有问题 select TO_BASE64('张三') a1,TO_BASE64('Hello') a2,TO_BASE64('http://www.baidu.com') a3; select FROM_BASE64('5byg5LiJ') a1,FROM_BASE64('SGVsbG8=') a2,FROM_BASE64('aHR0cDovL3d3dy5iYWlkdS5jb20=') a3;
-- 大小写转换 select LCASE('Hello') a0,LOWER('Hello') a1,UCASE('Hello') a2,UPPER('Hello') a3; select LCASE('张三') a0,LOWER('张三') a1,UCASE('张三') a2,UPPER('张三') a3;
-- 截取左边字符串 select LEFT('Hello',3) a1,LEFT('Hello',10) a2; -- 截取右边字符串 select RIGHT('Hello',3) a1,RIGHT('Hello',10) a2;
-- 截取字符串,长度不够左边补齐 select LPAD('Hello',10,'#') a1,LPAD('Hello',3,'#') a2; -- 截取字符串,长度不够右边补齐 select RPAD('Hello',10,'#') a1,RPAD('Hello',3,'#') a2;
select LTRIM(' Hello ') a1,LPAD(' Hello ',3,'#') a2; select RTRIM(' Hello ') a1,LPAD(' Hello ',3,'#') a2;
-- 对中文支持不好 select ASCII('A') a1,ASCII('a') a2,ASCII('张') a3; select CHAR(65) a1,CHAR(77,121,83,81,'76') a2,CHAR(229) a3;
select REVERSE('Hello') a1, REVERSE('张三') a2;
REPLACE(
str
,from_str
,to_str
)
select REPLACE('Hello张三','H','李四') a1,REPLACE('Hello','l','AA') a2;
REPEAT(
str
,count
)
select REPEAT('张三',2) a1,REPEAT('ABC',2) a2;
SUBSTRING(
, str
,pos
)SUBSTRING(
, str
FROM pos
)SUBSTRING(
, str
,pos
,len
)SUBSTRING(
str
FROMpos
FOR len
)
SUBSTR(
, str
,pos
)SUBSTR(
, str
FROM pos
)SUBSTR(
, str
,pos
,len
)SUBSTR(
str
FROM pos
FOR len
)
select SUBSTRING('Hello World',3) a1,SUBSTRING('Hello',3,20) a2; select SUBSTR('Hello World',3) a1,SUBSTR('Hello',3,20) a2;
STRCMP(
expr1
,expr2
)
SELECT STRCMP('1','2') a1,STRCMP('1','1') a2,STRCMP('2','1') a3; SELECT STRCMP('A','ABC') a1,STRCMP('1','A') a2,STRCMP('B','ABC') a3;
INSTR(
str
,substr
)
LOCATE(
,substr
,str
)
LOCATE(
substr
,str
,pos
)
POSITION(
substr in
str
)
select INSTR('Hello','A') a1,INSTR('Hello','ll') a2; select LOCATE('A','Hello') a1,LOCATE('ll','Hello') a2,LOCATE('a','abcabc',2) a3; select POSITION('A' in 'Hello') a1,POSITION('ll' in 'Hello') a2;