Kiran
Kiran

Reputation: 57959

SQL Server : DATEDIFF Start and End date

I get a positive value of 100 for the following query. Why is it? I was expecting -100.

SELECT DATEDIFF(SECOND, GETUTCDATE(), DATEADD(SECOND, 100, GETUTCDATE()))

From the documentation here in MSDN, the format of DATEDIFF function is below

DATEDIFF (datepart, startdate, enddate)

So, I am not sure what am I missing.

Upvotes: 0

Views: 1884

Answers (2)

Radu Gheorghiu
Radu Gheorghiu

Reputation: 20499

Well, this is because time "flows" in a single direction, from an older date to a more recent.

Thus, it would be normal to assume that startdate is equivalent to "days since" and enddate is equivalent with "days until".

This way, it does make sense that wanting to see the time/date difference between an older date to a more recent date will generate a positive number (because of the "flow of time"). Otherwise, you're moving in the "opposite flow" and you're going to get a negative number.

Upvotes: 4

Hano Johannes Rossouw
Hano Johannes Rossouw

Reputation: 770

It compares the size of the second relative to the first date. Thus the second date being 100 seconds bigger is why you are receiving your answer.

For reference : http://www.w3schools.com/sql/func_datediff.asp

Upvotes: 2

Related Questions