Get start_date/end_date of current week, month, quarter in MySQL

select curDate(); #Get the current date select curTime(); #Get the current time select now(); #Get the current date + time


Get start_date/end_date of current week, month, quarter in MySQL

 

The first day of the current week: 

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY) 

 

Last day of the current week: 

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 5 DAY) 

 

The first day of the previous week: 

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 8 DAY) 

 

Last day of the previous week: 

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 2 DAY) 

 

The first day of the first two weeks: 

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 15 DAY) 

 

Last day of the first two weeks: 

select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 9 DAY) 

 

 

The first day of the current month: 

SELECT concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01') 

 

The last day of the current month: 

SELECT LAST_DAY(now()) 

 

The first day of the previous month: 

SELECT concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01') 

 

The last day of the previous month: 

SELECT LAST_DAY(now() - interval 1 month) 

 

The first day of the first two months: 

SELECT concat(date_format(LAST_DAY(now() - interval 2 month),'%Y-%m-'),'01') 

 

The last day of the first two months: 

SELECT LAST_DAY(now() - interval 2 month) 

 

 

The first day of the current quarter: 

select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-3 month),'%Y-%m-'),'01') 

 

Last day of the current quarter: 

select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-1 month) 

 

The first day of the previous quarter: 

select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-6 month),'%Y-%m-'),'01') 

 

Last day of the previous quarter: 

select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-4 month) 

 

The first day of the first two quarters: 

select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-9 month),'%Y-%m-'),'01') 

 

Last day of the first two quarters: 

select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-7 month)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326031971&siteId=291194637