一、字串函数
介绍 : 对字串(char或varchar)输入值执行运算,然后返回一个字串
先建立一个Function_str资料表,并插入数值
CREATE TABLE Function_str(str1 VARCHAR(40),str2 VARCHAR(40),str3 VARCHAR(40))BEGIN TRANSACTION;INSERT INTO Function_str(str1,str2,str3)VALUES('蒙其','D','鲁夫')INSERT INTO Function_str(str1,str2,str3)VALUES('橡胶','果实',NULL)INSERT INTO Function_str(str1,str2,str3)VALUES('Father',NULL,NULL)INSERT INTO Function_str(str1,str2,str3)VALUES('Monkey','D', 'Dragon')INSERT INTO Function_str(str1,str2,str3)VALUES('波特卡斯','D','艾斯')INSERT INTO Function_str(str1,str2,str3)VALUES('烧烧','果实',NULL)INSERT INTO Function_str(str1,str2,str3)VALUES('Father',NULL,NULL)INSERT INTO Function_str(str1,str2,str3)VALUES('Gol','D','Roger')COMMIT;
<<接下来会使用刚刚建立的Function_str资料表>>
● CONCAT函数 : 字串连接
★ 函数写法 : CONCAT (字串1,字串2,……)
Ex1 : 连接str1,str2,str3字串
SELECT str1,str2,str3,concat(str1,str2,str3)AS concat_strFROM Function_str
执行结果
● LEN函数 : 显示字串长度
Ex2 : 使用LENGTH函数来查询str1长度
SELECT str1,LEN(str1) AS length_strFROM Function_str
执行结果
● LOWER函数 : 转小写
Ex3 : 使用LOWER函数将str1转为小写
SELECT str1,LOWER( str1) AS lower_strFROM Function_str
执行结果(英文字母以外不会受影响)
● UPPER函数 : 转大写
Ex4: 使用UPPER函数将str1转为大写
SELECT str1,UPPER( str1) AS lower_strFROM Function_str
执行结果(英文字母以外不会受影响)
● SUBSTRING函数 : 字串撷取
★ 函数写法 : SUBSTRING(撷取对象字串 ,开始撷取位置 ,撷取文字数量)
Ex5 : 撷取出str3的第1、2位置的文字
SELECT str3,SUBSTRING(str3,1,2)FROM Function_str
执行结果