30 second offset with smalldatetime comparison on SQL

178 Views Asked by At

I've came across an odd problem while comparing dates (actually smalldatetime) in SQL Server. It seems like there is a 30 seconds offset/bias while comparing dates close to the end of the day:

This is correct:

enter image description here

But this is definitely not correct:

enter image description here

What is happening here? Is math broken?

2

There are 2 best solutions below

0
On BEST ANSWER

The smalldatetime type has a accuracy of one minute so the literal '2019-12-31 23:59:30' is rounded to '2020-01-01 00:00:00'.

1
On

If you read the [documentation][1] for smalldatetime, you will see this note:

https://learn.microsoft.com/en-us/sql/t-sql/data-types/smalldatetime-transact-sql?view=sql-server-2017

Accuracy One minute

In other words, the literal value is being rounded. As a result, what-you-see-is-not-what-you-get.