Utilizing the FROMPARTS Dates functions in SQL Server

SQL Server 2012 introduced an improved method for creating Date, DateTime, DateTime2, DateTimeOffset, SmallDateTime, and Time objects. These functions are also available in Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse.

These functions are particularly useful in scenarios where dates need to be dynamically generated based on separate inputs. The syntax is straightforward, below is a list of DateFrom functions along with examples.

DATETIMEFROMPARTS(year, month, day, hour, minute, seconds, milliseconds)

SELECT DATETIMEFROMPARTS(2025, 2, 18, 10, 0, 0, 0) as newdate
> 2025-02-18 10:00:00:000

DATEFROMPARTS(year, month, day)

SELECT DATEFROMPARTS(2025, 2, 18) as newdate
> 2025-02-18

DATETIME2FROMPARTS(year, month, day, hr, min, sec, fractions, precision)

SELECT DATETIME2FROMPARTS(2025, 2, 18, 10, 0, 0 ,0 ,0)
> 2025-02-18 10:00:00

DATETIMEOFFSETFROMPARTS(year, month, day, hr, min, sec, fraction, hr_offset, min_offset, precision)

SELECT DATETIMEOFFSETFROMPARTS(2025, 2, 18, 10, 0, 0, 0, -7, 0 0)
> 2025-02-18 10:00:00 -7:00

SMALLDATETIMEFROMPARTS(year, month, day, hour, min)

SELECT SMALLDATETIMEFROMPARTS(2025, 2, 18, 10, 0, 0)
> 2025-02-18 10:00:00

TIMEFROMPARTS(hour, min, sec, fractions, precision)

SELECT TIMEFROMPARTS(10,30, 0, 0 ,0)
> 10:30:00


Leave a Reply

Your email address will not be published. Required fields are marked *