DATEFROMPARTS() in SQL
Creates a date value from specified year, month, and day integers.
Syntax
Return type
DATEFROMPARTS() Function Example
What is DATEFROMPARTS() in SQL?
The DATEFROMPARTS()
function in SQL creates a date value from specified year, month, and day integer inputs. It is primarily used for constructing date values dynamically, handling user input, and generating specific dates for queries.
This function is available in SQL Server. For other databases:
MySQL & PostgreSQL: Use
MAKE_DATE(year, month, day)
.Oracle: Use
TO_DATE(year || '-' || month || '-' || day, 'YYYY-MM-DD')
.SQLite: Use
DATE(year || '-' || month || '-' || day)
.
This function is useful in data entry validation, dynamic date calculations, and constructing date-based reports.
Parameters:
year: Integer value for year (0001-9999)
month: Integer value for month (1-12)
day: Integer value for day (1-31)
Example Use Cases:
-- Create a specific date
-- Create dates dynamically
-- Filter records after constructed date
Notes:
Behavior: Returns NULL if any input is NULL
Performance Considerations: Simple integer operations, generally efficient
Version Info: Not available in all DBMS
Deprecated/Recommended Alternatives: Some DBMS use MAKE_DATE() or DATE()
Error Handling:
Error: Returns NULL or error for invalid date components
Recommendation: Validate year, month, and day values before construction
Supported Databases:
DBMS
Function / Syntax
Example
Behavior with NULL
MySQL
MAKEDATE()
MAKEDATE(2024, day_of_year)
Returns NULL
PostgreSQL
MAKE_DATE()
MAKE_DATE(2024, 12, 26)
Returns NULL
SQL Server
DATEFROMPARTS()
DATEFROMPARTS(2024, 12, 26)
Returns NULL
SQLite
date()
date(2024, 12, 26)
Returns NULL
BigQuery
DATE()
DATE(2024, 12, 26)
Returns NULL
Snowflake
DATE_FROM_PARTS()
DATE_FROM_PARTS(2024, 12, 26)
Returns NULL
Athena
date()
date('2024-12-26')
Returns NULL