Generate A Date From Day, Month & Year In SQL Server

As a database programmer sometime you may get values of day, month and year in integer format. You may need to use these values to generate a date in date datatype. For this purpose you can use the built-in function DATEFROMPARTS. This function was introduced in SQL Server 2012. This function accepts day, month and year values in integer and returns a date. Let’s see how to use DATEFROMPARTS.

Syntax

DATEFROMPARTS ( <year>, <month>, <day> )

Where:
<year> = Years in integer, 
<month> = Months in integer, 
<day> = Days in integer

Example

DECLARE @year INT;
DECLARE @month INT;
DECLARE @day INT;

SET @year = 2016
SET @month = 7
SET @day = 25

SELECT DATEFROMPARTS(@year, @month, @day);
GO

/* Result */
2016-07-25
Generate A Date From Day, Month & Year

Reference


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.