{"id":4305,"date":"2015-12-12T00:57:34","date_gmt":"2015-12-11T19:27:34","guid":{"rendered":"http:\/\/sqlhints.com\/?p=4305"},"modified":"2015-12-23T07:07:29","modified_gmt":"2015-12-23T01:37:29","slug":"datediff_big-function-in-sql-server-2016","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2015\/12\/12\/datediff_big-function-in-sql-server-2016\/","title":{"rendered":"DATEDIFF_BIG Function in Sql Server 2016"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>DATEDIFF_BIG<\/strong> is one of the new function introduced in <strong>Sql Server 2016<\/strong>. It gives the difference between the two dates in the units specified by the DatePart parameter and the returned unit is of type bigint. This function like DATEDIFF function returns the number of the specified datepart boundaries crossed between the specified startdate and enddate. The difference between these two functions is the return type. DATEDIFF functions return type is INT, whereas the DATEDIFF_BIG functions return type is BIGINT.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nDATEDIFF_BIG ( datepart , startdate , enddate )\r\n<\/pre>\n<p><strong>[ALSO READ] <a href=\"https:\/\/sqlhints.com\/2015\/12\/12\/difference-between-datediff-and-datediff_big-functions-in-sql-server\/\" target=\"_blank\">Difference between DATEDIFF and DATEDIFF_BIG functions in Sql Server<\/a><\/strong><\/p>\n<p style=\"text-align: justify;\">The request for this new function was submitted on the <a href=\"https:\/\/connect.microsoft.com\/SQLServer\/feedback\/details\/320998\/add-datediff-big\" target=\"_blank\">Microsoft connect site<\/a> some time back in 2008 by Erland Sommarskog.<\/p>\n<p style=\"text-align: justify;\">With DATEDIFF function for milliseconds the maximum difference between startdate and enddate is 24 days, 20 hours, 31 minutes and 23.647 seconds. For second, the maximum difference is 68 years. This is because the return type of the DATEDIFF function is INT and INT datatypes Min and Max value is: -2,147,483,648 to +2,147,483,647. But with DATEDIFF_BIG function the maximum difference is very high as the return type is bigint and it\u2019s Min and Max value is: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.<\/p>\n<p><strong>Example 1: Basic example<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nSELECT DATEDIFF_BIG(DAY, GETDATE(), GETDATE()+1) &#039;DateDiff Big&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2015\/12\/DATEDIFF_BIG-Basic-example.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2015\/12\/DATEDIFF_BIG-Basic-example.jpg\" alt=\"DATEDIFF_BIG Basic example\" width=\"500\" height=\"127\" class=\"alignnone size-full wp-image-4306\" \/><\/a><\/p>\n<p><strong>ALSO READ:<\/strong> <a href=\"https:\/\/sqlhints.com\/2015\/07\/10\/how-to-get-difference-between-two-dates-in-years-months-and-days-in-sql-server\/\" target=\"_blank\">How to get difference between two dates in Years, Months and days in Sql Server<\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Example 2:<\/strong> Below example demonstrates how DATEDIFF and DATEDIF_BIG functions behave differently when the milliseconds difference between two dates is greater than the INT max (i.e. 2,147,483,647) value.<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nSELECT DATEDIFF(ms, &#039;2015-12-01&#039;, &#039;2015-12-31&#039;) &#039;DateDiff&#039;\r\n<\/pre>\n<p><strong>RESULT:<\/strong><\/p>\n<p style=\"text-align: justify;color: red;\">Msg 535, Level 16, State 0, Line 1<br \/>\nThe datediff function resulted in an overflow. The number of dateparts separating two date\/time instances is too large. Try to use datediff with a less precise datepart.<\/p>\n<pre class=\"brush: sql; gutter: false\">\r\nSELECT DATEDIFF_BIG(ms, &#039;2015-12-01&#039;, &#039;2015-12-31&#039;) &#039;DateDiff&#039; \r\n<\/pre>\n<p><strong>RESULT:<\/strong><br \/>\n<a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2015\/12\/DATEDIFF_BIG-Sql-Example.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2015\/12\/DATEDIFF_BIG-Sql-Example.jpg\" alt=\"DATEDIFF_BIG Sql Example\" width=\"500\" height=\"129\" class=\"alignnone size-full wp-image-4307\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">So, if we know that sometime the difference between two dates is going to cross the INT max value then we have to use the DATEDIFF_BIG function<\/p>\n<p><strong>[ALSO READ]:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/10\/25\/json-in-sql-server-2016\/\" target=\"_blank\">Native JSON Support in Sql Server 2016<\/a>\n<ul>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/15\/for-json-clause-in-sql-server-2016\/\" target=\"_blank\">FOR JSON Clause in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/22\/openjson-function-in-sql-server-2016\/\" target=\"_blank\">OPENJSON Function in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/15\/isjson-function-in-sql-server-2016\/\" target=\"_blank\">ISJSON Function in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/15\/json_value-function-in-sql-server-2016\/\" target=\"_blank\">JSON_VALUE Function in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/15\/json_query-function-in-sql-server-2016\/\" target=\"_blank\">JSON_QUERY Function in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/15\/lax-and-strict-json-path-modes-in-sql-server-2016\/\" target=\"_blank\">lax and strict JSON Path modes in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/28\/indexing-strategy-for-json-value-in-sql-server-2016\/\" target=\"_blank\">Indexing Strategy for JSON Value in Sql Server 2016<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/12\/drop-if-exists-statement-in-sql-server-2016\/\" target=\"_blank\">DROP IF EXISTS Statement in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/11\/29\/compare-execution-plan-in-sql-server-2016\/\" target=\"_blank\">Compare Execution Plans in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/12\/11\/live-query-statistics-in-sql-server-2016\/\" target=\"_blank\">Live Query Statistics in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/12\/12\/datediff_big-function-in-sql-server-2016\/\" target=\"_blank\">DATEDIFF_BIG Function in Sql Server 2016<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/12\/12\/difference-between-datediff-and-datediff_big-functions-in-sql-server\/\" target=\"_blank\">Difference between DATEDIFF and DATEDIFF_BIG functions in Sql Server<\/a><\/li>\n<li><a href=\"https:\/\/sqlhints.com\/2015\/12\/15\/session_context-in-sql-server-2016\/\" target=\"_blank\">SESSION_CONTEXT in Sql Server 2016<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>DATEDIFF_BIG is one of the new function introduced in Sql Server 2016. It gives the difference between the two dates in the units specified by the DatePart parameter and the returned unit is of type bigint. This function like DATEDIFF function returns the number of the specified datepart boundaries crossed between the specified startdate and &hellip; <a href=\"https:\/\/sqlhints.com\/2015\/12\/12\/datediff_big-function-in-sql-server-2016\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">DATEDIFF_BIG Function in Sql Server 2016<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3,830],"tags":[954,958,957,955,956,952,321,835,986,834,953],"class_list":["post-4305","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-sql-server-2016","tag-datediff_big","tag-datediff_big-function-in-sql","tag-datediff_big-in-sql","tag-datediff_big-in-sql-2016","tag-datediff_big-in-sql-server-2016","tag-msg-535-level-16-state-0-line-1","tag-sql","tag-sql-2016","tag-sql-server","tag-sql-server-2016","tag-the-datediff-function-resulted-in-an-overflow"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-17r","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/4305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=4305"}],"version-history":[{"count":11,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/4305\/revisions"}],"predecessor-version":[{"id":4372,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/4305\/revisions\/4372"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=4305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=4305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=4305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}