曾经也做过几年的 数据开发,都会用 时间函数做为变量重刷数据 。比如重刷去年的数据,重刷当月的数据等 今天整理一下干货
前言
DATE_FORMAT 是 MySQL 中用于格式化日期的函数。它允许你将日期按照指定的格式进行显示。以下是 DATE_FORMAT 函数的基本语法:
DATE_FORMAT(date, format)
date 是要格式化的日期值。 format 是用于指定日期格式的格式字符串。 下面是一些常用的日期格式元素,它们可以在 format 字符串中使用:
| Specifier | Description |
|---|---|
| %a | Abbreviated weekday name (Sun..Sat) |
| %b | Abbreviated month name (Jan..Dec) |
| %c | Month, numeric (0..12) |
| %D | Day of the month with English suffix (0th, 1st, 2nd, 3rd, …) |
| %d | Day of the month, numeric (00..31) |
| %e | Day of the month, numeric (0..31) |
| %f | Microseconds (000000..999999) |
| %H | Hour (00..23) |
| %h | Hour (01..12) |
| %I | Hour (01..12) |
| %i | Minutes, numeric (00..59) |
| %j | Day of year (001..366) |
| %k | Hour (0..23) |
| %l | Hour (1..12) |
| %M | Month name (January..December) |
| %m | Month, numeric (00..12) |
| %p | AM or PM |
| %r | Time, 12-hour (hh:mm:ss followed by AM or PM) |
| %S | Seconds (00..59) |
| %s | Seconds (00..59) |
| %T | Time, 24-hour (hh:mm:ss) |
| %U | Week (00..53), where Sunday is the first day of the week; WEEK() mode 0 |
| %u | Week (00..53), where Monday is the first day of the week; WEEK() mode 1 |
| %V | Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X |
| %v | Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x |
| %W | Weekday name (Sunday..Saturday) |
| %w | Day of the week (0=Sunday..6=Saturday) |
| %X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
| %x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
| %Y | Year, numeric, four digits |
| %y | Year, numeric (two digits) |
| %% | A literal % character |
| %x | x, for any “x” not listed above |
一、当日
select
now() as start_time--当前日期
date_format(CURDATE(),'%Y-%m-%d') cur_date,-- 当日
date_format(CURDATE(),'%Y-%m') cur_yyyymm-- 当日对应的年月
二、昨日_1
select
date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m-%d') last_date, -- 昨日
date_format(date_sub(CURDATE(), interval 1 day),'%Y%m%d') last_date8, -- 昨日yyyymmdd格式
date_format(DATE_SUB(CURDATE(), interval 3 day),'%Y-%m-%d') last_day_2, -- 昨日前推2天
date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m') as cur_month,-- 昨日对应的月份
date_format(date_sub(CURDATE(), interval 1 day),'%Y') as cur_year,-- 昨日对应的年份
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-1 year),'%Y') last_year,-- 昨日对应的去年
三、昨日_2
select
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval 1 month),'%Y-%m') as next_month,-- 昨日对应月份的下一个月
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval 2 month),'%Y-%m') as next_month2,-- 昨日对应月份的下2个月
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval -1 month),'%Y-%m') as last_month,-- 昨日对应月份的上一个月
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval -2 month),'%Y-%m') as last_month2,-- 昨日对应月份的上一个月
date_sub(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),interval 1 day) as end_date_last_month, -- 昨日对应上月的月底时间,即昨日对应月初-1天
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 3 month),'%Y-%m') last_month_3, -- 昨日对应的当月前推3个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 6 month),'%Y-%m') last_month_6, -- 昨日对应的当月前推6个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 9 month),'%Y-%m') last_month_9, -- 昨日对应的当月前推9个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 12 month),'%Y-%m') last_month_12, -- 昨日对应的当月前推12个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 24 month),'%Y-%m') last_month_24 -- 昨日对应的当月前推24个月
三、昨日_3
select
date_format(date_sub(CURDATE(), interval 8 day),'%Y-%m-%d') last_date_7, -- 昨日的一个星期前
date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d')as first_month_date,-- 昨日的月初
date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y%m'),'01'),'%Y%m%d') as first_month_date8,-- 昨日的月初_yyymmdd格式
date_add(date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d'),interval 1 month) as next_first_month_date,-- 昨日的下月初1号
date_format(date_sub(date_add(date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d'),interval 1 month),interval 1 day),'%Y-%m-%d') as end_month_date,-- 昨日对应的本月月末最后一天
date_format(date_sub(date_add(date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d'),interval -11 month),interval 1 day),'%Y-%m-%d') as last_end_month_date,-- 昨日对应的去年本月月末最后一天
concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01') as first_month_year,-- 昨日对应的本年第一个月
concat(date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 1 year),'%Y'),'-01') as first_month_last_year-- 昨日对应的去年第一个月
三、昨日_4
select
date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y-%m-%d') as day_of_last_year,---- 昨日对应的去年相同年月日
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-1 year),'%Y-%m') yyyymm_of_last_year,-- 昨日对应的去年同样年月
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-2 year),'%Y-%m-%d') as day_of_last_2year, -- 昨日对应的前年同样日期
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-2 year),'%Y-%m') yyyymm_of_last_2year, -- 昨日对应的前年同样年月
date_sub(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01-01'), interval 1 day) as last_end_year,-- 昨日对应去年的最后一天
date_sub(date_sub(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01-01'), interval 1 day),interval 1 year) as last_end_year2,-- 昨日对应前去年的最后一天
date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01-01'),'%Y-%m-%d') as first_year_date -- 昨日对应前今年的第一天
三、昨日_5
select
date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y-%m-%d') as last_date1,-- 昨日对应的去年今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date1,-- 昨日对应的去年年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date1,-- 昨日对应的去年第一天
date_format(date_sub(date_sub(CURDATE(),interval 2 year),interval 1 day),'%Y-%m-%d') as last_date2,-- 昨日对应的前年今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 2 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date2,-- 昨日对应的前年的年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 2 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date2,-- 昨日对应的前年第一天
date_format(date_sub(date_sub(CURDATE(),interval 3 year),interval 1 day),'%Y-%m-%d') as last_date3, -- 昨日对应的往前推 3年的 今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 3 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date3,-- 昨日对应的往前推3年的年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 3 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date3,-- 昨日对应的往前推3年第一天
date_format(date_sub(date_sub(CURDATE(),interval 4 year),interval 1 day),'%Y-%m-%d') as last_date4, -- 昨日对应的往前推 4年的 今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 4 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date4, -- 昨日对应的往前推4年的年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 4 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date4 -- 昨日对应的往前推4年第一天
总结
以上就整理的根据昨天或今日计算出年,月,日。
应用场景:
转换日期格式以适应特定的应用需求。 生成报告中的易读日期字符串。 在将日期数据导出到外部系统时,确保日期格式的一致性。
ETL 示例:
ETL 过程,从源表提取数据,进行转换,然后加载到目标表
INSERT INTO destination_table (id, formatted_date)
SELECT
id,
DATE_FORMAT(original_date, '%Y-%m-%d') AS formatted_date
FROM
source_table;