mysql 日期处理成财年财季财月财周

mysql 日期处理成财年财季财月财周,第1张

财年是一个公司的上市时间决定的,所以不同的公司财年码表不同,因为我目前用的财年是从4月1号开始算的,自己就写了个mysql 的工具函数

SELECT

-- 自然日

naturalday,

-- 财年

concat('FY', DATE_FORMAT(naturalday,'%y'),  DATE_FORMAT(naturalday,'%y')+1) as fiscalyear,

-- 财季

concat('Q', case when QUARTER(naturalday)=4 then 1 ELSE QUARTER(naturalday)-1 END

) as fiscalquarter,

-- 财月

DATE_FORMAT(naturalday,'%m') as fiscalmonth,

-- 财周

timestampdiff(WEEK,date(concat(year(naturalday),'-',elt(quarter(naturalday),1,4,7,10),'-',1)),naturalday) as fiscalweek

from tool_lenovo_day

一下是我写的一个存储过程:

CREATE DEFINER=`root`@`%` PROCEDURE `sp_InitialFiscalYear_data`(`sDate` varchar(20),`eDate` varchar(20))

BEGIN

/*

工具函数:生成时间段内的lenovo的财年、财季、财月、财周数据

做成:白纸黑字

做成日:2020-07-16

入参:

sDate 开始自然日

eDate 结束自然日

业务逻辑:

tool_lenovo_day 财年表

有更新、没有新增财年数据

*/

INSERT INTO tool_lenovo_day SELECT

*

FROM

(

SELECT

naturalday,

concat( 'FY', DATE_FORMAT( naturalday, '%y' ), DATE_FORMAT( naturalday, '%y' )+ 1 ) AS fiscalyear,

concat( 'Q', CASE WHEN QUARTER ( naturalday )= 4 THEN 1 ELSE QUARTER ( naturalday )- 1 END ) AS fiscalquarter,

DATE_FORMAT( naturalday, '%m' ) AS fiscalmonth,

timestampdiff(

WEEK,

date(

concat( YEAR ( naturalday ), '-', elt( QUARTER ( naturalday ), 1, 4, 7, 10 ), '-', 1 )),

naturalday

)+1 AS fiscalweek

FROM

(

SELECT

date_add( date_sub(sDate,interval 1 day), INTERVAL ( cast( help_topic_id AS signed INTEGER ) + 1 ) day ) naturalday

FROM

mysql.help_topic

WHERE

help_topic_id <DATEDIFF(eDate,date_sub(sDate,interval 1 day))

ORDER BY

help_topic_id

) as s_e_day

) AS a

ON DUPLICATE KEY UPDATE fiscalyear = a.fiscalyear,

fiscalquarter = a.fiscalquarter,

fiscalmonth = a.fiscalmonth,

fiscalweek = a.fiscalweek

END

select curDate()#获取当前日期select curTime()#获取当前时间select now()#获取当前日期+时间

列举1个天数加减的例子,其他的看英文意思就可以理解了

select date_add(now(), interval 1 day) #当前日期天数+1

select date_add(now(), interval -1 day) #当前日期天数-1

select date_add(now(), interval 1 hour)

select date_add(now(), interval 1 minute)

select date_add(now(), interval 1 second)

select date_add(now(), interval 1 microsecond)

select date_add(now(), interval 1 week)

select date_add(now(), interval 1 month)

select date_add(now(), interval 1 quarter)

select date_add(now(), interval 1 year)

1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()

mysql>select current_timestamp, current_timestamp()

+---------------------+---------------------+

| current_timestamp | current_timestamp() |

+---------------------+---------------------+

| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |

+---------------------+---------------------+

2. MySQL (Unix 时间戳、日期)转换函数:

unix_timestamp(),

unix_timestamp(date),

from_unixtime(unix_timestamp),

from_unixtime(unix_timestamp,format)

下面是示例:

select unix_timestamp()-- 1218290027

select unix_timestamp('2008-08-08')-- 1218124800

select unix_timestamp('2008-08-08 12:30:00')-- 1218169800

select from_unixtime(1218290027)-- '2008-08-09 21:53:47'

select from_unixtime(1218124800)-- '2008-08-08 00:00:00'

select from_unixtime(1218169800)-- '2008-08-08 12:30:00'

select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x')-- '2008 8th August 12:30:00 2008'

3. MySQL 时间戳(timestamp)转换、增、减函数:

timestamp(date) -- date to timestamp

timestamp(dt,time) -- dt + time

timestampadd(unit,interval,datetime_expr) --

timestampdiff(unit,datetime_expr1,datetime_expr2) --

请看示例部分:

select timestamp('2008-08-08')-- 2008-08-08 00:00:00

select timestamp('2008-08-08 08:00:00', '01:01:01')-- 2008-08-08 09:01:01

select timestamp('2008-08-08 08:00:00', '10 01:01:01')-- 2008-08-18 09:01:01

select timestampadd(day, 1, '2008-08-08 08:00:00')-- 2008-08-09 08:00:00

select date_add('2008-08-08 08:00:00', interval 1 day)-- 2008-08-09 08:00:00

MySQL timestampadd() 函数类似于 date_add()。

select timestampdiff(year,'2002-05-01','2001-01-01')-- -1

select timestampdiff(day ,'2002-05-01','2001-01-01')-- -485

select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00')-- -12

select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00')-- 7

MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7313640.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存