I need to change the Date of my database, considering the Business Date used in the company I work for.
Here, a business month is between 26/M-1/AAAA
and 25/M/AAAA
.
Where M-1: last month.
For example, today the trade date is 26/08/2017
and 25/09/2017
.
But the problem is in the range of 26 to 31 (or 30 or 28, last day of the month), because in this range, the trade date must be 26/M/AAAA
and 25/M+1/AAAA
, and when it turns the month, it will be 26/M-1/AAAA
and 25/M/AAAA
again.
I created a query for this, but the problem in the above range could not resolve.
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'
;