How to use the MySQL MAKEDATE() function

MySQL MAKEDATE() is a function that returns a date by taking a value of a year and a number of days.

Posted on

MySQL MAKEDATE() is a function that returns a date by taking a value of a year and a number of days. The number of days must be greater than 0 otherwise a NULL will be returned. This function is useful for generating dates for specific occasions or events that occur on certain days of the year.

Syntax

The syntax of the function is:

MAKEDATE(year, dayofyear)

The parameters are:

  • year: The year for which you want to create a date. It must be a four-digit year.
  • dayofyear: The day of the year for which you want to create a date. It must be a positive integer from 1 to 366.

Examples

Some examples of using the function are:

  • To get the date of the third day of 2023, use:

    SELECT MAKEDATE(2023, 3);
    

    The result is:

    2023-01-03
    

    This means that the date of the third day of 2023 is January 3, 2023.

  • To get the date of the 175th day of 2023, use:

    SELECT MAKEDATE(2023, 175);
    

    The result is:

    2023-06-24
    

    This means that the date of the 175th day of 2023 is June 24, 2023.

  • To get the date of the 366th day of 2024, use:

    SELECT MAKEDATE(2024, 366);
    

    The result is:

    2024-12-31
    

    This means that the date of the 366th day of 2024 is December 31, 2024. Note that 2024 is a leap year, so it has 366 days.

  • To get the date of the 100th day of the current year, use:

    SELECT MAKEDATE(YEAR(CURDATE()), 100);
    

    The result is:

    2023-04-10
    

    This means that the date of the 100th day of the current year is April 10, 2023.

  • To get the date of the last day of the current year, use:

    SELECT MAKEDATE(YEAR(CURDATE()), DAYOFYEAR(LAST_DAY(CURDATE())));
    

    The result is:

    2023-12-31
    

    This means that the date of the last day of the current year is December 31, 2023.

Similar Functions

Some similar functions to MAKEDATE() are:

  • DATE(): This function returns the date part of a datetime expression.
  • DATE_FORMAT(): This function formats a date or datetime value according to a specified format string.
  • DAYOFYEAR(): This function returns the day of the year for a given date or datetime value, from 1 to 366.
  • LAST_DAY(): This function returns the last day of the month for a given date or datetime value.