The thing is that internally datetime data is stored as 8 byte consisting of 2 integers: one that represents days after 19000101
and other representing ticks after midnight. So actually you are substracting 2 integers here:
Day of DT1 - Day of DT2
Ticks of DT1 - Ticks of DT2
Days are 0s since 19000101
is saved internally as 0
. So first substraction gives you 0
, i.e. 19000101
.
The difference of ticks will give you 16
hours, but pay attension, it will be ticks(16
hours) from midnight, so actually you should substract 16
hours from midnight not 7am
. Thus 00:00 - 16 hours
is equal to 08:00
. Of course here you shoulb decrement the result of substruction of day, i.e. 0-1=18991231
.
So you end up with 18991231 08:00
and this is correct output.
But you should be very careful when using operators for date manipulation because there are nuances. Use native functions for such manipulation, like DATEADD
. You can read this article, especially from page 2 http://www.devx.com/dbzone/Article/34594/0/page/2