Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

The Evolution of DATEDIFFBIG in SQL SERVER 2016 #4

$
0
0

In the series of SQL SERVER 2016, this is a new post. in this post, we will discuss DATEDIFF_BIG and how it is helpful.

So, before jumping into directly in technical details, we all know that time is very important and every second valuable and countable but sometimes every microsecond & nanosecond is also countable
The Evolution of DATEDIFFBIG in SQL SERVER 2016 #4
. For such operations in which every microsecond & nanosecond is countable, we can use DATEDIFF_BIG function.

As you aware the BIGINT range is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Here if any difference (Micro & Nano) second is out of the the mentioned range then DATEDIFF returns that value else return error (Obviously).

Below is the basic syntax if DATEDIFF_BIG although it is similar to DATEDIFF. We can say it is a extended version of DATEDIFF.

DATEDIFF_BIG( datePart, start Date, End date)

The value of datePart is same like DATEDIFF function.

For example if you want to collect millisecond difference then use ms , microsecond then mcs and for nanosecond ns.

As per the MSDN for the Millisecond, the maximum difference between start date & end date is 24 days, 20 hours, 21 minutes and 23,647 seconds. For Second, the maximum difference is 68 years.

Now, let see why this DATEDIFF_BIG introduced so, I am running a DATEDIFF function in SQL SERVER 2012 and see what we get after running that query.


The Evolution of DATEDIFFBIG in SQL SERVER 2016 #4

You can see in above query we got an error of overflow.

Now, we are calculating the same difference from DATEDIFF_BIG in SQL SERVER 2016. See, below snap for same.


The Evolution of DATEDIFFBIG in SQL SERVER 2016 #4
Isn’t it great ? Although, I am scarred with those applications who calculate milliseconds
The Evolution of DATEDIFFBIG in SQL SERVER 2016 #4
.

Anyways, it is good to know feature.

Do provide your feedback for the post it is very valuable for us.

RJ !!!


Viewing all articles
Browse latest Browse all 3160

Latest Images

Trending Articles