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
本季度的第一天 ,然后你可以把字符串再转换为日期。 方法很多,包括同一个函数的别名(同义词)也很多。比如CURDATE(),CURRENT_DATE(), CURRENT_DATE, NOW 等都可以返回当天mysql教程>select CURDATE(),ELT(QUARTER(CURDATE()),
-> year(CURDATE())*1000+0101,
-> year(CURDATE())*1000+0401,
-> year(CURDATE())*1000+0701,
-> year(CURDATE())*1000+1001) as firstDayofQ
+------------+-------------+
| CURDATE() | firstDayofQ |
+------------+-------------+
| 2009-05-19 | 2009401 |
+------------+-------------+
1 row in set (0.00 sec)
mysql>
用mysql语句获取本季度的第一天
本月的第一天,
date(
concat(year(curdate()),'-',month(curdate()),'-','1'))
本周的第一天
curdate()-
WEEKDAY(curdate())
用mysql语句获取本季度的第一天
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)