SQL SERVER

DATEFROMPARTS function in SQL Server

SQL Server Function
Avatar of shohal
Written by shohal

DATEFROMPARTS function in SQL Server

The DATEFROMPARTS() function returns a date from the specified parts (year, month, and day values).

DATEFROMPARTS function

  • Introduced in SQL Server 2012
  • Returns a date value for the specified year, month, and day
  • The data type of all the 3 parameters (year, month, and day) is integer
  • If invalid argument values are specified, the function returns an error
  • If any of the arguments are NULL, the function returns null

Syntax : DATEFROMPARTS ( year, month, day )

Example : All the function arguments have valid values, so DATEFROMPARTS returns the expected date

SELECT DATEFROMPARTS ( 2015, 10, 25) AS [Date]

Output :

datefromparts function in sql server
DATEFROMPARTS function in SQL Server | TechTweet

Example : Invalid value specified for month parameter, so the function returns an error

SELECT DATEFROMPARTS ( 2015, 15, 25) AS [Date]

Output : Cannot construct data type date, some of the arguments have values which are not valid.
Example : NULL specified for month parameter, so the function returns NULL.

SELECT DATEFROMPARTS ( 2015, NULL, 25) AS [Date]

Output : 

datefromparts in sql server

Other new date and time functions introduced in SQL Server 2012

  • EOMONTH (Discussed in Part 125 of SQL Server tutorial)
  • DATETIMEFROMPARTS : Returns DateTime
  • Syntax : DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds )
  • SMALLDATETIMEFROMPARTS : Returns SmallDateTime
  • Syntax : SMALLDATETIMEFROMPARTS ( year, month, day, hour, minute )
  • We will discuss the following functions in a later video
    • TIMEFROMPARTS
    • DATETIME2FROMPARTS
    • DATETIMEOFFSETFROMPARTS

In our next video we will discuss the difference between DateTime and SmallDateTime.

For More : https://www.youtube.com/channel/UC4DdVKXrecFoGY_R7D_RYNQ/playlists

About the author

Avatar of shohal

shohal

I have profession and personal attachment with custom ERP Software development, Business Analysis, Project Management and Implementation almost (36) ,also Oracle Apex is my all-time favorite platform to developed the software. Moreover i have some website development experience with WordPress. For hand on networking experience DevOps and CCNA, it create me a full package. Here are some core programming language with networking course i have been worked: Oracle SQL ,PL/SQL,Oracle 19c Database , Oracle Apex 20.1,WordPress,Asp.Net ,MS SQL ,CCNA ,Dev Ops, SAP SD

Leave a Comment