18.字符串函数
1.CHARSET(str)
返回字符串字符集
2.CONCAT(string[str1,str2,str3...])
连接字符串
3.INSTR(string,substring)
返回子段在字符串中的位置,没有返回0
4.UCASE(string)
转大写
5.LCASE(string)
转小写
6.LEFT(string1,length)
从string1左边起取length个字符
7.LENGTH(string)
求字符串长度
8.REPLACE(str , search_str , replace_str)
替换子串
9.STRCMP(str1 , str2)
比较字符串大小
比较规则
- 字母大小写敏感:例如
'A'
小于'a'
(因为 ASCII 值不同) - 逐字符比较:从左到右比较每个字符的 ASCII 值
- NULL 值处理:任何字符串与
NULL
比较结果都是NULL
STRCMP(str1, str2)
函数用于比较两个字符串的大小,返回值规则如下:
- 0:如果两个字符串相等
- -1:如果
str1
小于str2
- 1:如果
str1
大于str2
10.SUBSTRING(str , position , [,length])
从str的position处开始,取length个字符
说明:如果不写length,则从position处取后面的所有子串。
11.LTRIM(str)
去前空格
12.RTRIM(str)
去后空格
13.TRIM(str)
去左右空格
示例
(1)先建立一张测试用表
-- 创建测试表
CREATE TABLE string_functions_test (col1 VARCHAR(50),col2 VARCHAR(50),col3 VARCHAR(50),col4 VARCHAR(50),col5 VARCHAR(50)
);
插入测试用数据
INSERT INTO string_functions_test (col1, col2, col3, col4, col5) VALUES
('Hello', 'WORLD', ' Leading Space', 'This is a test', '12345'),
('MySQL', 'Strings', 'Trailing Space ', 'TESTING', 'abcde'),
('Functions', 'Test', ' Both Spaces ', 'mixedCase', NULL),
('Special Chars', '!@#$%^&*()', 'NoSpaces', 'UPPERandlower', '中文测试'),
(NULL, 'NULL Value', ' ', 'Empty?', '');
结果:
(2)返回字符串的字符集
-- 1. CHARSET(str) - 返回字符串字符集
SELECT col1, CHARSET(col1) AS charset FROM string_functions_test;
(3)拼接字符串
-- 2. CONCAT(string[str1,str2,str3...]) - 连接字符串
SELECT col1, col2, CONCAT(col1, ' ', col2) AS concatenated FROM string_functions_test;
(4)返回子串的位置
-- 3. INSTR(string, substring) - 返回子段在字符串中的位置,没有返回0
SELECT col4, INSTR(col4, 'test') AS position FROM string_functions_test;
(5)转大写
-- 4. UCASE(string) - 转大写
SELECT col2, UCASE(col2) AS uppercase FROM string_functions_test;
(6)转小写
-- 5. LCASE(string) - 转小写
SELECT col1, LCASE(col1) AS lowercase FROM string_functions_test;
(7)左取
-- 6. LEFT(string1, length) - 从string1左边起取length个字符
SELECT col4, LEFT(col4, 10) AS left_chars FROM string_functions_test;
(8)求字符串长度
-- 7. LENGTH(string) - 求字符串长度
SELECT col3, LENGTH(col3) AS length FROM string_functions_test;
(9)替换子串
-- 8. REPLACE(str, search_str, replace_str) - 替换子串
SELECT col4, REPLACE(col4, 'test', 'REPLACED') AS replaced FROM string_functions_test;
(10)比较字符串大小
-- 9. STRCMP(str1, str2) - 比较字符串大小
SELECT col1, col2, STRCMP(col1, col2) AS comparison FROM string_functions_test;
说明:
对于该测试数据,STRCMP(col1, col2)
的结果如下:
col1 | col2 | comparison | 说明 |
'Hello' | 'WORLD' | -1 | H(72) < W(87) |
'MySQL' | 'Strings' | -1 | M(77) < S(83) |
'Functions' | 'Test' | -1 | F(70) < T(84) |
'Special Chars' | '!@#$%^&*()' | 1 | S(83) > !(33) |
NULL | 'NULL Value' | NULL | 任何与 NULL 比较都是 NULL |
(11)从位置取
-- 10. SUBSTRING(str, position, [,length]) - 从str的position处开始,取length个字符
SELECT col4, SUBSTRING(col4, 5, 10) AS substring FROM string_functions_test;
SELECT col4, SUBSTRING(col4, 5) AS substring FROM string_functions_test;
(12)去前空格
-- 11. LTRIM(str) - 去前空格
SELECT col3, LTRIM(col3) AS ltrimmed FROM string_functions_test;
(13)去后空格
-- 12. RTRIM(str) - 去后空格
SELECT col3, RTRIM(col3) AS rtrimmed FROM string_functions_test;
(14)去左右空格
-- 13. TRIM(str) - 去左右空格
SELECT col3, TRIM(col3) AS ltrimmed FROM string_functions_test;