{"id":2515,"date":"2018-06-13T16:18:41","date_gmt":"2018-06-13T23:18:41","guid":{"rendered":"https:\/\/database.guide\/?p=2515"},"modified":"2021-01-28T14:11:28","modified_gmt":"2021-01-28T04:11:28","slug":"datefromparts-examples-in-sql-server-t-sql","status":"publish","type":"post","link":"https:\/\/database.guide\/datefromparts-examples-in-sql-server-t-sql\/","title":{"rendered":"DATEFROMPARTS() Examples in SQL Server (T-SQL)"},"content":{"rendered":"<p>You can use the <a href=\"https:\/\/database.guide\/what-is-t-sql\/\">T-SQL<\/a> <code>DATEFROMPARTS()<\/code> function in <a href=\"https:\/\/database.guide\/what-is-sql-server\/\">SQL Server<\/a> to return a <strong>date<\/strong> value that maps to the date parts that you provide as integer expressions.<\/p>\n<p>The way it works is, you provide three values (one for each of the <em>year<\/em>, <em>month<\/em>, and <em>day<\/em> values), and SQL Server will return a <strong>date<\/strong> value based on the values you provide.<\/p>\n<p>Examples below.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>First, here&#8217;s the syntax:<\/p>\n<pre>DATEFROMPARTS ( year, month, day )<\/pre>\n<p>Here&#8217;s what each argument can be:<\/p>\n<dl>\n<dt><code>year<\/code><\/dt>\n<dd>An integer expression that specifies a year.<\/dd>\n<dt><code>month<\/code><\/dt>\n<dd>An integer expression that specifies a month, from 1 to 12.<\/dd>\n<dt><code>day<\/code><\/dt>\n<dd>An integer expression that specifies a day.<\/dd>\n<\/dl>\n<h2>Example<\/h2>\n<p>Here&#8217;s an example of usage:<\/p>\n<pre>SELECT DATEFROMPARTS( 2021, 05, 10 ) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+------------+\r\n| Result     |\r\n|------------|\r\n| 2021-05-10 |\r\n+------------+\r\n<\/pre>\n<h2>Invalid Values<\/h2>\n<p>You&#8217;ll need to ensure that the date parts are provided in the correct order, otherwise you could end up with an error. Worse yet, you might not get an error (if the day portion is 12 or less), and therefore could miss a major problem with the way the date is translated.<\/p>\n<p>Here&#8217;s an example of an invalid value:<\/p>\n<pre>SELECT DATEFROMPARTS( 2021, 30, 10 ) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>Cannot construct data type date, some of the arguments have values which are not valid. \r\n<\/pre>\n<p>This occurs because we provided 30 as the month part. This part will only accept values between 1 and 12.<\/p>\n<h2>Null Values<\/h2>\n<p>If any of the arguments are null values, the result is <code>NULL<\/code>.<\/p>\n<p>Here&#8217;s an example using a null value:<\/p>\n<pre>SELECT DATEFROMPARTS( 2021, NULL, 10 ) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n|----------|\r\n| NULL     |\r\n+----------+\r\n<\/pre>\n<p>Therefore, it probably goes without saying (but I&#8217;ll say it anyway), that if all arguments are null values, the result is <code>NULL<\/code>:<\/p>\n<pre>SELECT DATEFROMPARTS( NULL, NULL, NULL ) AS Result;<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n|----------|\r\n| NULL     |\r\n+----------+\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can use the T-SQL DATEFROMPARTS() function in SQL Server to return a date value that maps to the date parts that you provide as integer expressions. The way it works is, you provide three values (one for each of the year, month, and day values), and SQL Server will return a date value based &#8230; <a title=\"DATEFROMPARTS() Examples in SQL Server (T-SQL)\" class=\"read-more\" href=\"https:\/\/database.guide\/datefromparts-examples-in-sql-server-t-sql\/\" aria-label=\"Read more about DATEFROMPARTS() Examples in SQL Server (T-SQL)\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5],"tags":[68,115,93,77,61],"class_list":["post-2515","post","type-post","status-publish","format-standard","hentry","category-sql","category-sql-server","tag-date-functions","tag-dates","tag-functions","tag-mssql","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=2515"}],"version-history":[{"count":3,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2515\/revisions"}],"predecessor-version":[{"id":2518,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2515\/revisions\/2518"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}