Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATEADD MS -1 does nothing

This is my basic "test"

select DATEADD(ms,-2,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
        , DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
        ,case when DATEADD(ms,-2,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) != DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) then 'No Match' else 'Match' end 
union all 
select DATEADD(ms,-1,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
       ,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
       ,case when DATEADD(ms,-1,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)) != DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) then 'No Match' else 'Match' end 

I am trying to understand why MS -2 , subtracts 3 and MS -1 subtracts none.

like image 883
Holmes IV Avatar asked Aug 12 '15 18:08

Holmes IV


1 Answers

MSSQL's datetime data type has a finest granularity of .00333333s (repeating), or roughly 3 milliseconds. Changes less than that will result in either no change, or rounded to 3.

From https://msdn.microsoft.com/en-us/library/cc280460.aspx

datetime2(3) has a precision of one millisecond, and datetime has a precision of 1/300 of a second.

like image 171
Lynn Crumbling Avatar answered Nov 02 '22 08:11

Lynn Crumbling