Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first day of every corresponding month in mysql?

Tags:

date

mysql

I want to get first day of every corresponding month of current year. For example, if user selects '2010-06-15', query demands to run from '2010-06-01' instead of '2010-06-15'.

Please help me how to calculate first day from selected date. Currently, I am trying to get desirable using following mysql select query:

Select
  DAYOFMONTH(hrm_attendanceregister.Date) >=
  DAYOFMONTH(
    DATE_SUB('2010-07-17', INTERVAL - DAYOFMONTH('2010-07-17') + 1 DAY
  )
FROM
  hrm_attendanceregister;

Thanks

like image 231
David Avatar asked Jul 21 '10 10:07

David


People also ask

How can I get first Monday of the month in MySQL?

SET @firstday = '2013-04-01'; SELECT ADDDATE( @firstday , MOD((9-DAYOFWEEK(@firstday)),7)) as first_monday; The param @firstday is the first day of the month you want to search. Note that sunday is the first day of a week, monday is the second day. The answer was helpful.

How can I get month start and end date in SQL?

Syntax : = EOMONTH(start_date, months) If we set parameter months as 0, then EOMONTH will output the last day of the month in which StartDate falls.

How can I calculate start date and end date in MySQL?

Solution. To determine the date for the first day in a month, use date shifting (an application of date arithmetic). To determine the date for the last day, use the LAST_DAY() function. To determine the number of days in a month, find the date for the last day and use it as the argument to DAYOFMONTH() .


3 Answers

Is this what you are looking for:

select CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') as DATE);
like image 103
Krunal Avatar answered Oct 20 '22 13:10

Krunal


You can use the LAST_DAY function provided by MySQL to retrieve the last day of any month, that's easy:

SELECT LAST_DAY('2010-06-15');

Will return:

2010-06-30

Unfortunately, MySQL does not provide any FIRST_DAY function to retrieve the first day of a month (not sure why). But given the last day, you can add a day and subtract a month to get the first day. Thus you can define a custom function:

DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
  RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;

That way:

SELECT FIRST_DAY('2010-06-15');

Will return:

2010-06-01
like image 45
Stéphane Avatar answered Oct 20 '22 15:10

Stéphane


There is actually a straightforward solution since the first day of the month is simply today - (day_of_month_in_today - 1):

select now() - interval (day(now())-1) day

Contrast that with the other methods which are extremely roundabout and indirect.


Also, since we are not interested in the time component, curdate() is a better (and faster) function than now(). We can also take advantage of subdate()'s 2-arity overload since that is more performant than using interval. So a better solution is:

select subdate(curdate(), (day(curdate())-1))
like image 30
Pacerier Avatar answered Oct 20 '22 13:10

Pacerier