企业级应用 mysql 日期函数变量,干货已整理

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介: 本文详细介绍了如何在MySQL8.0中使用DATE_FORMAT函数进行日期格式的转换,包括当日、昨日及不同时间段的数据获取,并提供了实际的ETL应用场景和注意事项,有助于提升数据处理的灵活性和一致性。

企业级应用 mysql 8.0 日期函数变量,干货已整理

曾经也做过几年的 数据开发,都会用 时间函数做为变量重刷数据 。比如重刷去年的数据,重刷当月的数据等

今天整理一下干货


前言

[具体可以访问 mysql Data_format 帮助]

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;

注意事项:

日期格式字符串中的各种元素之间可以添加自定义字符,如空格、短横线等,以获得所需的格式。


要确保格式字符串与实际日期数据的格式相匹配,以避免错误的格式化结果。

总体而言,DATE_FORMAT 函数在处理日期数据时是一个非常有用的工具,可以根据具体需求灵活调整格式,提高数据的可读性和适应性。


最后

希望大家喜欢 一建3连 ,初十快乐


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
7月前
|
关系型数据库 MySQL 分布式数据库
安全可靠的PolarDB V2.0 (兼容MySQL)产品能力及应用场景
PolarDB分布式轻量版采用软件输出方式,能够部署在您的自主环境中。PolarDB分布式轻量版保留并承载了云原生数据库PolarDB分布式版技术团队深厚的内核优化成果,在保持高性能的同时,显著降低成本。
667 140
|
5月前
|
SQL 监控 关系型数据库
MySQL事务处理:ACID特性与实战应用
本文深入解析了MySQL事务处理机制及ACID特性,通过银行转账、批量操作等实际案例展示了事务的应用技巧,并提供了性能优化方案。内容涵盖事务操作、一致性保障、并发控制、持久性机制、分布式事务及最佳实践,助力开发者构建高可靠数据库系统。
|
4月前
|
关系型数据库 MySQL Unix
MySQL中日期和时间函数的使用指南
使用这些函数可以有效地处理和分析日期和时间数据,对于数据库管理、报表生成和数据分析非常关键。在实际应用中,根据具体需求选择适当的函数进行数据处理,可以极大地提高数据处理的效率和准确性。
319 17
|
6月前
|
存储 关系型数据库 MySQL
MYSQL数据加密压缩函数应用实战指南。
总的来说,加密和压缩是维护MySQL数据库中数据安全性和效率的有效手段。使用时需权衡性能与安全,合理应用加密和压缩函数。在设计数据库架构时要考虑到加密字段的查询性能,因为加密可能使得一些索引失效。压缩数据能有效减少存储空间的占用,但在服务器负载较高时应避免实时压缩和解压,以免影响总体性能。
221 10
|
6月前
|
存储 关系型数据库 MySQL
MySQL数据库中进行日期比较的多种方法介绍。
以上方法提供了灵活多样地处理和对比MySQL数据库中存储地不同格式地日子信息方式。根据实际需求选择适当方式能够有效执行所需操作并保证性能优化。
653 10
|
7月前
|
存储 SQL 人工智能
MySQL 数据类型详解:字符串、数字、日期
在 MySQL 数据库设计中,选择合适的数据类型对存储效率和查询性能至关重要。本文详细介绍了字符串、数字和日期三大类数据类型及其子类型,帮助开发者根据业务需求做出更优选择。内容涵盖 CHAR 与 VARCHAR 的区别、TEXT 和 BLOB 的使用场景、整数与浮点类型的适用范围,以及日期时间类型的特性。通过最佳实践建议,提升数据库性能并避免常见问题。
399 4
|
8月前
|
SQL 关系型数据库 MySQL
MySQL 常用函数
我们这次全面梳理 MySQL 中的常用函数,涵盖 聚合函数、字符串函数、日期时间函数、数学函数 和 控制流函数 等五大类。每类函数均配有语法说明与实用示例,帮助读者提升数据处理能力,如统计分析、文本处理、日期计算、条件判断等。文章结尾提供了丰富的实战练习,帮助读者巩固和应用函数技巧,是进阶 SQL 编程与数据分析的实用工具手册。
600 2
|
9月前
|
关系型数据库 MySQL 定位技术
MySQL与Clickhouse数据库:探讨日期和时间的加法运算。
这一次的冒险就到这儿,期待你的再次加入,我们一起在数据库的世界中找寻下一个宝藏。
379 9
|
11月前
|
SQL 关系型数据库 MySQL
【YashanDB知识库】MySQL field 函数的改写方法
【YashanDB知识库】MySQL field 函数的改写方法
|
SQL 关系型数据库 MySQL
Mysql-常用函数及其用法总结
以上列举了MySQL中一些常用的函数及其用法。这些函数在日常的数据库操作中非常实用,能够简化数据查询和处理过程,提高开发效率。掌握这些函数的使用方法,可以更高效地处理和分析数据。
346 19

推荐镜像

更多