In this article, we will learn how to get the total number of days in a month in MySQL.
There are many ways to get the total number of days in a month by using the current date or by using a specific date. Here are some examples using a current date or using a specific date to get the total number of days in a month.
Approach 1: In this approach, we are using the DAY()
function and LAST_DAY()
Function to get the total number of days in a month.
Example 1: To get the total number of days in a month by using the current date function NOW()
;
-- This method returns the total no. of days in a month using current Date.
SELECT DAY(LAST_DAY(now())) AS 'Total No. of Days in a Month';
Example 2: To get the total number of days in a month by using a specific date;
-- This method returns the total no. of days in a month using a specific date.
SELECT DAY(LAST_DAY('2020-01-20')) as 'Total No. of Days in a Month';
This query will return 31.
Approach 2: In this approach, we are using the DAYOFMONTH()
function and LAST_DAY()
Function to get the total number of days in a month.
Example 1: To get the total number of days in a month by using the current date function NOW()
;
-- This method returns the total no. of days in a month using a current date.
SELECT DAYOFMONTH(LAST_DAY(now())) as 'Total No. of Days in a Month';
Example 2: To get the total number of days in a month by using a specific date;
-- This method returns the total no. of days in a month using a Specific Date.
SELECT DAYOFMONTH(LAST_DAY('2020-01-20')) as 'Total No. of Days in a Month';
This query will return 31.
To Get the Total Number of days in a month till date, here is the example:
-- This method returns the total no. of days in a month to date.
SELECT DAYOFMONTH('2020-01-20') AS 'Total No. of Days in a Month till date';
This query will return 20.
-- This method returns the total no. of days in a month to date.
SELECT DAY('2020-01-20') AS 'Total No. of Days in a Month till date';
This query will return 20.
I hope this article will help you to understand how to get the total number of days in a month in MySQL.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments