一、字符串函数
1、concat(str1, str2, …) 拼接多个字符串。
select concat('hello', ' ', 'world'); -- 输出 'hello world' select concat(数字, '%'); -- 输出 百分数
2、substring(str, start, length) 或 substr() 截取字符串。
select substring('mysql', 3, 2); -- 输出 'sq'
3、length(str) 与 char_length(str) 获取字符串的长度
length():返回字节数(受字符集影响);char_length():返回字符数。
select length('中国'), char_length('中国'); -- 输出 6(utf-8)和 2
4、upper(str) / lower(str) 转换大小写
select upper('mysql'); -- 输出 'mysql'
5、replace(str, old_str, new_str) 替换字符串
select replace('apple pie', 'apple', 'cherry'); -- 输出 'cherry pie'
6、trim([leading|trailing|both] ‘char’ from str) 去除首尾指定字符(默认去空格)
select trim(' mysql '); -- 输出 'mysql' 去除空格字符
- trim() #删除前后空格
- rtrim() #删除字符串结尾空格
- ltrim() #删除字符串起始空格
both删除指定的首尾字符
select trim(both 'a' from 'applea'); --结果pple
leading 删除指定的首字符
select trim(leading 'a' from 'applea'); --结果pplea
trailing删除指定的尾字符
select trim(trailing 'a' from 'applea'); --结果apple
二、时间函数
1、now() 返回当前日期和时间,格式 yyyy-mm-dd hh:mm:ss
select now(); -- 2023-10-25 14:30:45
2、curdate() 返回当前日期,格式 yyyy-mm-dd
select curdate(); -- 2023-10-25
3、curtime() 返回当前时间,格式 hh:mm:ss
select curtime(); -- 14:30:45
4、date() 仅存储日期(年-月-日),不包含时间。 yyyy-mm-dd(例如:2023-10-05)。
create table example (event_date date); insert into example values ('2023-10-05');
5、time 仅存储时间(时:分:秒),可以表示一天中的时间点或时间间隔。
格式 hh:mm:ss(例如:15:30:00)。支持微秒:hh:mm:ss[.fraction](例如:15:30:00.123456)
create table example (duration time); insert into example values ('15:30:00'), ('-05:00:00');
6、datetime() 存储日期 + 时间(年-月-日 时:分:秒),转换之后的格式是字符串的格式
格式:yyyy-mm-dd hh:mm:ss(例如:2023-10-05 15:30:00)。支持微秒:yyyy-mm-dd hh:mm:ss[.fraction]。
create table example (event_time datetime); insert into example values ('2023-10-05 15:30:00');
7、常用日期格式化符号
符号 | 含义 |
---|---|
%y | 四位年份 |
%m | 两位月份 (01-12) |
%m | 完整英文名称(january, february, …, december)。 |
%d | 两位日期 (01-31) |
%d | 表示日期(月中的第几天)的 英文序数后缀格式(1st, 2nd, 3rd, 4th, …, 30th, 31st)。 |
%h | 24小时制小时 (00-23) |
%i: | 分钟 (00-59) |
%s: | 秒 (00-59) |
%w: | 星期全名 (sunday-saturday) |
%a: | 缩写星期 (sun-sat) |
%t | 24小时制时间(等价于 %h:%i:%s) |
%r | 12小时制时间(等价于 %h:%i:%s %p) |
%p | 上午/下午标记(am/pm) |
8、日期/时间格式化,将日期转换成字符串格式
- date_format(date, format): 格式化日期,日期和时间部分
select date_format(now(), '%y-%m-%d %h:%i:%s'); -- 2023-10-25 14:30:45 select date_format(now(), '%w, %m %e, %y'); -- wednesday, october 25, 2023 select date_format(now(), '%y年%m月%d日 %h时%i分'); -- 2023年10月25日 14时30分
- time_format(time, format): 格式化时间(仅时间)(用法同 date_format)
select time_format('2023-10-01 08:30:00', '%t'); -- 输出: 08:30:00
9、str_to_date(str, format) 将字符串解析为日期
select str_to_date('25,10,2023', '%d,%m,%y'); -- 2023-10-25
10、日期计算
- date_add(date, interval expr unit): 日期加法
select date_add(now(), interval 7 day); -- 当前时间加7天 select date_add(now(), interval 3 month); -- 当前时间加3个月
- date_sub(date, interval expr unit): 日期减法
select date_sub(now(), interval 1 hour); -- 当前时间减1小时
- datediff(date1, date2): 计算两个日期之间的天数差
select datediff('2023-10-31', '2023-10-25'); -- 6
其中unit 可选值如下year、month、day、hour、minute、second,并且只能使用%y-%m-%d格式,不能使用%y-%m-%d格式
11、提取日期部分
year(date): 提取年份
month(date): 提取月份
day(date): 提取日期
hour(time): 提取小时
minute(time): 提取分钟
second(time): 提取秒
extract(unit from date)
select extract(year_month from '2023-10-01'); -- 202310 mysql> select extract(year_month from now()); +--------------------------------+ | extract(year_month from now()) | +--------------------------------+ | 202504 | +--------------------------------+ 1 row in set (0.01 sec) mysql> mysql> select extract(year_month from now()); +--------------------------------+ | extract(year_month from now()) | +--------------------------------+ | 202504 | +--------------------------------+ 1 row in set (0.01 sec) mysql>
总结
到此这篇关于mysql常用字符串函数和时间函数的文章就介绍到这了,更多相关mysql字符串函数和时间函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论