MySQL MAKEDATE() Function
The MAKEDATE()
function in MySQL constructs a date value from a year and a day number within that year. This is particularly useful for generating dates programmatically, especially when you're working with day numbers rather than month and day values.
MAKEDATE(): Definition and Usage
MAKEDATE()
is a handy function for creating dates, especially when you're dealing with data that represents days since the start of the year. The function takes a year (as a four-digit integer) and the day of the year (an integer from 1 to 366) as input and returns the corresponding date. Make sure that the day number is valid for the specified year (leap years have 366 days, non-leap years 365).
Syntax
Syntax
MAKEDATE(year, day)
Parameter Values
Parameter | Description |
---|---|
year |
The year (a four-digit integer). This is required. |
day |
The day of the year (an integer from 1 to 366, where 1 is January 1st). This is required. |
Examples
Creating a Date from Year and Day Number
This example creates the date for the third day of 2017 (January 3rd, 2017).
Syntax
SELECT MAKEDATE(2017, 3);
Output
2017-01-03
Creating Dates for Different Day Numbers
These examples demonstrate creating dates for different day numbers within the year 2017. Remember that 2017 was not a leap year.
Syntax
SELECT MAKEDATE(2017, 175); --June 21st
SELECT MAKEDATE(2017, 100); --April 10th
SELECT MAKEDATE(2017, 365); --December 31st
Output
2017-06-21
2017-04-10
2017-12-31