SQL MAKEDATE function
Returns a date given a year and a day number within that year, from 1 to 366.
- Catégorie
- Dates & times
- Returns
date- In this category
- 26 of 121
Syntaxe
MAKEDATE(year, dayofyear)
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Different name or syntax | DATEADD(day, n-1, DATEFROMPARTS(year, 1, 1)) |
| PostgreSQL | Different name or syntax | make_date(year, 1, 1) + (n - 1) |
| MySQL | Oui | Supported under this name. |
| SQLite | Different name or syntax | date(year || '-01-01', '+' || (n - 1) || ' days') |
| Oracle | Different name or syntax | TO_DATE(year || LPAD(n, 3, '0'), 'YYYYDDD') |
Exemples
MySQL
SELECT MAKEDATE(2024, 60) AS leap_day; -- 2024-02-29
MySQL
SELECT MAKEDATE(2024, 0) AS nothing; -- NULL
SQL Server
SELECT DATEADD(day, 60 - 1, DATEFROMPARTS(2024, 1, 1)) AS leap_day;
Bon à savoir
- Day 0 returns NULL and days past the year's end spill into the next year: MAKEDATE(2024, 367) is 2025-01-01.
- It is not a month-and-day constructor: MAKEDATE(2024, 3) is January 3, not March. That mix-up is the classic bug.
- Non-leap years cap at 365: MAKEDATE(2023, 366) becomes 2024-01-01, silently shifting the year.
Questions about MAKEDATE
How do I build a date from month and day in MySQL?
Not with MAKEDATE alone. STR_TO_DATE(CONCAT_WS('-', y, m, d), '%Y-%m-%d') works, or MAKEDATE(y, 1) + INTERVAL (m - 1) MONTH + INTERVAL (d - 1) DAY.
What does MAKEDATE return for day 0?
NULL. Day numbering starts at 1; watch for computed day-of-year values that can hit zero.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.