`Exercises:`
函数
字符串函数
函数 是指一段可以直接被另一段程序调用的程序或代码。 |
|
通过函数我们可以利用数据库中的信息进行一些数据的计算。 |
常见的字符串函数
函数 | 功能 |
---|
concat(s1,s2,...sn) | 字符串拼接,将s1,s2,…sn拼接成一个字符串 |
lower(str) | 将字符串str全部转为小写 |
upper(str) | 将字符串str全部转换为大写 |
lpad(str,n,pad) | 左填充 ,用字符串pad对str的左边进行填充,达到n个字符串长度 |
rpad(str,n,pad) | 右填充 ,用字符串pad对str的右边进行填充,达到n个字符串长度 |
trm(str) | 去掉字符串头部和尾部的空格(注意:只是去除头部和尾部空格,中间不去除) |
substring(str,start,len) | 返回从字符串str从start位置起的len个长度的字符串 |
字符串拼接
-- 字符串函数
-- concat
select concat('hello' , ' Aileen');

字符串大小写转换
-- lower
select lower('Happy New Years!');

-- upper
select upper('Happy New Years!');

字符串左右填充
lpad
select lpad('ling', 5 , '❤');

-- rpad
select rpad('ling' , 5 , '❤');

删除左右空格
trim
select trim(' Good Luck! ');

字符串截取
-- substring 字符串截取
select substring('Hello MySQL',7,5);

Exercises:
1.由于业务需求变更,企业员工的工号,统一5位数,目前不是5位数的全部在前面补0。比如: 1号员工的工号应该为00001. |
|
update emp set workno = lpad(workno,5,0);

,第10张](/upload/website_attach/202402/1_59BG24F6QJ8JZ284.gif)
,第11张](/upload/website_attach/202402/1_6DCZ93YRCANY3PK4.gif)