select *
from A
where posted_date >= '2015-07-27 00:00:00.000'
and posted_date <= '2015-07-27 23:59:59.999'
But the result contains a record that has posted_date today: 2015-07-28.
My database server is not in my country.
What is the problem ?
Best Answer
Since you are using datetime datatype, you need to understand how sql server rounds datetime data.
Using below query, you can easily see the problem of rounding that sql server does when you use
DATETIME
data type.click to enlarge
DATETIME2
has been around since SQL Server 2008, so start using it instead ofDATETIME
. For your situation, you can usedatetime2
with precision of 3 decimals e.g.datetime2(3)
.Benefits of Using
datetime2
:datetime
supporting only 3 decimal places .. and hence you see the rounding issue since by defaultdatetime
rounds the nearest.003 seconds
with increments of.000
,.003
or.007
seconds.datetime2
is much more precise thandatetime
anddatetime2
gives you control ofDATE
andTIME
as opposed todatetime
.Reference :