1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
#1.LENGTH:获取参数值的字节个数
SELECT LENGTH('Mike'); #返回4
SELECT LENGTH('张三丰'); #返回9,在UTF8下,一个汉字占3个字节;在JDK下,一个汉字占2个字节
SHOW VARIABLES LIKE '%char%'; #显示所用字符集
#2.CONCAT: 拼接字符串
SELECT CONCAT(first_name, '*', last_name) AS '姓名'
FROM employees;
#3.UPPER、LOWER(将字符转换大小写):
SELECT UPPER('mike');
SELECT LOWER('MIKE');
#示例:将姓变大写,名变小写,然后拼接
SELECT CONCAT(UPPER(first_name), ' * ', LOWER(last_name)) AS '姓名'
FROM employees;
#4.SUBSTR、SUBSTRING(截取字符串,注:索引从1开始)
#带2个参数:截取从指定索引处后面所有字符
SELECT SUBSTR('李莫愁爱上了陆展元', 7) AS out_put; #返回陆展元
#带3个参数:截取从指定索引处指定字符长度的字符
SELECT SUBSTR('李莫愁爱上了陆展元', 1, 3) AS out_put; #返回李莫愁
#案例:姓名中首字符大写,其他字符小写然后用_拼接,显示出来
SELECT
CONCAT(
UPPER( SUBSTR(first_name, 1,1)),
LOWER( SUBSTR(first_name,2)))
AS out_put
FROM employees;
#5.INSTR:返回子串第一次出现的索引,如果找不到返回0
SELECT INSTR('杨不悔爱上了殷六侠', '殷六侠') AS out_put;
#6.TRIM:去除字符前后的空格或其他指定的字符
SELECT TRIM(' 张 翠 山 ') AS out_put;
SELECT TRIM('a' FROM'aaaaaaa张aa翠aaa山aaaaaa' ) AS out_put;
#7.LPAD:用指定的字符实现左填充至指定长度
SELECT LPAD('殷素素', 10, '*') AS out_put; #返回'*******殷素素'
SELECT LPAD('殷素素', 2, '*') AS out_put; #返回'殷素'
#8.RPAD:用指定的字符实现右填充至指定长度
SELECT RPAD('殷素素', 12, 'ab') AS out_put; #返回'殷素素ababababa'
SELECT RPAD('殷素素', 2, '*') AS out_put; #返回'殷素'
#9.REPLACE:替换
SELECT REPLACE('张无忌爱上了周芷若', '周芷若', '赵敏') AS out_put; #返回'张无忌爱上了赵敏'
SELECT REPLACE('张无忌周芷若爱上了周芷若', '周芷若', '赵敏') AS out_put; #返回'张无忌赵敏爱上了赵敏'
|