将日期更改为商业日期MyS

时间:2017-09-04 18:34:45

标签: mysql sql date datetime

考虑到我工作的企业中使用的商业日期,我需要更改数据库的日期。

此处,商业月份介于YYYY-(M-1)-26YYYY-M-25之间。 哪个M-1:上个月。

例如,今天商业日期为2017-08-262017-09-25

但问题是在26到31(或30或28,该月的最后一天)范围内,因为在此范围内,商业日期应为YYYY-M-26YYYY-(M+1)-25以及何时每月结束时,它会再次YYYY-(M-1)-26YYYY-M-25

我为此创建了一个查询,但上面范围内的问题无法解决。

select 
concat(
(       -- se mes tem 31 dias
        if(month(current_date()) in (1,3,5,7,8,10,12),
            -- então, diff(now() - data_i) <= 5? Se sim, incrementa +1 no mês, se não deixa -1
            if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 5, date_format(concat(year(current_date()),'-',month(current_date())-0,'-',26),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())-1,'-',26),'%d/%m/%Y')),
        -- se mes n tem 30 dias, ele tem 30 dias?
        if(month(current_date()) in (4,6,9,11),
            -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1
            if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 4, date_format(concat(year(current_date()),'-',month(current_date())-0,'-',26),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())-1,'-',26),'%d/%m/%Y')),
        -- mes 29
        if(month(current_date()) in (2),
            -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1
            if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 3, date_format(concat(year(current_date()),'-',month(current_date())-0,'-',26),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())-1,'-',26),'%d/%m/%Y')),
        999)))
    )
,' a ',
(       -- se mes tem 31 dias
        if(month(current_date()) in (1,3,5,7,8,10,12),
            -- então, diff(now() - data_i) <= 5? Se sim, incrementa +1 no mês, se não deixa -1
            if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 5, date_format(concat(year(current_date()),'-',month(current_date())+1,'-',25),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())+0,'-',25),'%d/%m/%Y')),
        -- se mes n tem 31 dias, ele tem 30 dias?
        if(month(current_date()) in (4,6,9,11),
            -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1
            if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 4, date_format(concat(year(current_date()),'-',month(current_date())+1,'-',25),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())+0,'-',25),'%d/%m/%Y')),
        if(month(current_date()) in (2),
            -- então, diff(now() - data_i) <= 4? Se sim, incrementa +1 no mês, se não deixa -1
            if(datediff(current_date(), date_format(concat(year(current_date()),'-',month(current_date()),'-',26),'%Y-%m-%d')) <= 3, date_format(concat(year(current_date()),'-',month(current_date())+1,'-',25),'%d/%m/%Y'),date_format(concat(year(current_date()),'-',month(current_date())+0,'-',25),'%d/%m/%Y')),
        999)))
    )
) as 'DataMesComercial'
;

1 个答案:

答案 0 :(得分:0)

我通过首先弄清楚我们进入&#34;商业月份的时间来简化问题&#34;以第25个为出发点。从那里,您可以根据您的描述确定是从M到M + 1,还是从M-1到M。

例如:( http://sqlfiddle.com/#!9/2228d7/41处的SQL小提琴)

SET @date = DATE('2016-04-21');
SET @commercial_day = DAY(@date)-25;
SET @commercial_month_start_offset = IF(@commercial_day<0, -1, 0);
SET @commercial_month_start = DATE_ADD(@date, INTERVAL @commercial_month_start_offset MONTH);
SET @commercial_month_end = DATE_ADD(@commercial_month_start, INTERVAL 1 MONTH);
SET @commercial_date_start = DATE_ADD(DATE_ADD(MAKEDATE(YEAR(@commercial_month_start), 1), INTERVAL (MONTH(@commercial_month_start))-1 MONTH), INTERVAL (25)-1 DAY );
SET @commercial_date_end = DATE_ADD(DATE_ADD(MAKEDATE(YEAR(@commercial_month_end), 1), INTERVAL (MONTH(@commercial_month_end))-1 MONTH), INTERVAL (26)-1 DAY );
SELECT @date, @commercial_date_start,@commercial_date_end;

我尝试了几个不同的日期,如果我理解正确的逻辑,相信这对你有用。您可以将其转换为返回开始日期或结束日期的MySQL函数,或者如上例所示的字符串。

我还在SQL Fiddle:http://sqlfiddle.com/#!9/634312/1

中创建了一个带有测试例程的函数

这应该允许您验证2017年的逻辑。