Back to Library

DATEFROMPARTS() in SQL

Creates a date value from specified year, month, and day integers.

Syntax

DATEFROMPARTS(year, month, day)
Return type
DATE

DATEFROMPARTS() Function Example

SELECT DATEFROMPARTS(2024, 12, 26);
-- Output: '2024-12-26'

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

SELECT DATEFROMPARTS(2024, 12, 31) AS new_years_eve;
-- Output:
new_years_eve
-------------
2024-12-31

-- Create dates dynamically

SELECT DATEFROMPARTS(year_col, month_col, day_col) FROM date_parts;
-- Output:
constructed_date
---------------
2024-12-31
2024-06-15
2024-09-30
2024-03-21
2024-11-05

-- Filter records after constructed date

SELECT * FROM orders 
WHERE order_date > DATEFROMPARTS(2024, 1, 1);
-- Output:
order_id    order_date    customer_id    amount    status
--------    ----------    -----------    ------    -------
O101        2024-12-15    C001          299.99    Shipped
O102        2024-11-30    C002          199.50    Complete
O103        2024-10-25    C003          449.99    Processing 
O104        2024-09-20    C004          599.99    Complete
O105        2024-08-15    C005          349.99    Shipped

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

Related Functions:

DATEFROMPARTS()

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

DATEFORMAT()

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

EOMONTH()

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

DAY()

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

MONTH()

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

YEAR()

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

DATEDIFF()

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

DATEADD()

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

GETDATE()

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

One place for all your queries,
directly on your SQL editor