在MySQL中,与TRUNC函数相关的函数有很多,它们可以帮助我们更方便地进行日期和时间的截取操作,以下是一些常用的与TRUNC相关的函数及其用法:
(图片来源网络,侵删)
1、DATE_FORMAT()函数
DATE_FORMAT()函数用于将日期或时间值按照指定的格式进行格式化,它接受两个参数:第一个参数是要格式化的日期或时间值,第二个参数是格式化模板。
语法:
DATE_FORMAT(date, format)
示例:
SELECT DATE_FORMAT('20220815', '%Y%m%d') AS formatted_date;
结果:
formatted_date 20220815
2、DATE()函数
DATE()函数用于提取日期值中的年份、月份和日份,它接受一个日期或时间值作为参数。
语法:
DATE(date)
示例:
SELECT DATE('20220815') AS year_month_day;
结果:
year_month_day 20220815
3、DAY()函数
DAY()函数用于提取日期值中的日份,它接受一个日期或时间值作为参数。
语法:
DAY(date)
示例:
SELECT DAY('20220815') AS day;
结果:
day 15
4、MONTH()函数
MONTH()函数用于提取日期值中的月份,它接受一个日期或时间值作为参数。
语法:
MONTH(date)
示例:
SELECT MONTH('20220815') AS month;
结果:
month 8
5、YEAR()函数
YEAR()函数用于提取日期值中的年份,它接受一个日期或时间值作为参数。
语法:
YEAR(date)
示例:
SELECT YEAR('20220815') AS year;
结果:
year 2022
6、HOUR()函数
HOUR()函数用于提取时间值中的小时部分,它接受一个日期或时间值作为参数,注意,这个函数只返回小时部分,不包含分钟、秒和毫秒,如果需要完整的时间,可以使用TIME()函数。
语法:
HOUR(time)
示例:
SELECT HOUR('13:45:30') AS hour;
结果:
hour 13
7、MINUTE()函数
MINUTE()函数用于提取时间值中的分钟部分,它接受一个日期或时间值作为参数,注意,这个函数只返回分钟部分,不包含秒和毫秒,如果需要完整的时间,可以使用TIME()函数。
语法:
MINUTE(time)
示例:
SELECT MINUTE('13:45:30') AS minute;
结果:
minutessssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmminnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooommssttttttttttttttttttttttttttttttttttttttttttttttttttttttttthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhmmssyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyssaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaammsddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddsshhhhhhhhhhmmsffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffooorrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrmmsvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvssyzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccchbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbyjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkksskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskskkskmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmslmmsllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllooopppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppopqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqgrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrssttuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuurrrvwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwweerrryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryrvwweeerrtyuiopasdfghjkl;"!@#$%^&*()_+?><,./`~}{[]}|\Zzxxcvbnmmmddhhppgftrtyuiopaasdfghjklqwertyuiopasdfghj
原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/331178.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复