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
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.
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.
You can use str_to_date to convert a date string to MySQL's internal date format for inserting.
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).
You just need to convert your values to a date. Here is one way:
select last_day(date(concat_ws('-', year, month, 1)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With