Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last day of month in mysql by giving month and year as input

How to get last day of the month in MySQL by providing month and year as input.

Similar example, To get last day of the month by date as input
    Eg: SELECT LAST_DAY(?) as lastDate

Format: 
Input month and year: '07/2015'
Output Result: day /*Eg: 30,31*/

I have tried with date Format SELECT LAST_DAY(DATE_FORMAT('09/2015','%m/%Y')) as lastDate but it din't work

like image 279
vickisys Avatar asked Oct 06 '15 11:10

vickisys


People also ask

How can we get previous month last day value in MySQL?

MySQL LAST_DAY() function MySQL LAST_DAY() returns the last day of the corresponding month for a date or datetime value. If the date or datetime value is invalid, the function returns NULL.

How do I get the last day of the month in SQL?

If you would like to get the date containing the last day of the month of a given date, use the EOMONTH() function. This function takes one mandatory argument: a date (or date and time), which can be a date/datetime column or an expression that returns a date.

How do I insert date in YYYY-MM-DD format in MySQL?

You can use str_to_date to convert a date string to MySQL's internal date format for inserting.

How do I get data from a specific month in SQL?

Use the MONTH() function to retrieve a month from a date/datetime/timestamp column in MySQL. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a date/datetime/timestamp column. (In our example, we use the start_date column of date data type).


1 Answers

You just need to convert your values to a date. Here is one way:

select last_day(date(concat_ws('-', year, month, 1)))
like image 100
Gordon Linoff Avatar answered Sep 19 '22 10:09

Gordon Linoff