Get the first and last day of the current week/month/quarter/year in MYSQL

http://my.oschina.net/zx0211/blog/698252


#The first day of the year:
SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY);

#The last day of the year:
SELECT concat(YEAR(now()),'-12-31');  

#The first day of the current week:  
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY);

#The last day of the current week:  
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 5 DAY);

#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);

#The 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');

#The 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://10.200.1.11:23101/article/api/json?id=326869313&siteId=291194637