SQL DATEFROMPARTS function
Returns a date from three integers. SQL Server has DATEFROMPARTS; PostgreSQL has make_date; MySQL, SQLite and Oracle rebuild from strings.
- Returns
date- In this category
- 22 of 121
語法
DATEFROMPARTS(year, month, day) -- SQL Server
MAKE_DATE(year, month, day) -- PostgreSQL
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | Different name or syntax | make_date(year, month, day) |
| MySQL | Different name or syntax | no direct form; MAKEDATE uses day-of-year, or STR_TO_DATE |
| SQLite | Different name or syntax | build a string and pass it to date() |
| Oracle | Different name or syntax | TO_DATE(year || '-' || month || '-' || day, 'YYYY-MM-DD') |
範例
SQL Server
SELECT DATEFROMPARTS(2024, 2, 29) AS leap_day;
PostgreSQL
SELECT make_date(2024, 2, 29) AS leap_day;
Oracle
SELECT TO_DATE('2024-02-29', 'YYYY-MM-DD') AS leap_day FROM dual;
小知識
- Invalid parts such as February 30 raise an error on SQL Server, PostgreSQL and Oracle. SQLite rolls date('2024-02-30') over to 2024-03-01 without complaint, and MySQL's STR_TO_DATE returns NULL.
- MySQL's MAKEDATE is not month-and-day: it takes day-of-year, so MAKEDATE(2024, 61) is March 1, not January 61.
- DATEFROMPARTS returns NULL when any argument is NULL, so a missing month in the source data becomes a missing date rather than an error.
Questions about DATEFROMPARTS
What happens with an invalid date like February 30?
SQL Server, PostgreSQL and Oracle raise an error. MySQL's STR_TO_DATE returns NULL in a SELECT (and errors inside an INSERT under strict mode); SQLite silently moves it to March 1.
Is DATEFROMPARTS the same as make_date?
Yes, same inputs and result. PostgreSQL spells it make_date; MySQL, SQLite and Oracle have no equivalent and need string building.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Get the first day of the month in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Get the first and last day of the year in SQL Snippet
- Cumulative total by month Snippet
- Get the first and last day of the month in SQL Snippet
- Get the first and last day of the quarter in SQL Snippet
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.