SQL

Find last day and length of month


Problem

As a programmer you might wish that every month had the same length, but things aren't that simple. Let's see what we can do in MySQL to find out the last day of the month and the number of days in a month.



Solution

Fairly simple, but maybe not always that obvious, last_day returns the date of the last day in the month and day returns the day portion from it.

SELECT
    last_day(now()) as last_day,
    day(last_day(now())) as days_per_month

Comments




Please sign-in to post a comment