Background
I recently learned that SQL Server's datetime type only stores time to approximately 1/300th of a second.
So given these data:
create table datetime_test (id int, my_date datetime);
insert into datetime_test (id, my_date) values
(0, '2020-12-11 17:14:07.000'),
(1, '2020-12-11 17:14:07.197'),
(2, '2020-12-11 17:14:07.198'),
(3, '2020-12-11 17:14:08.000');
This query would return (1, 2, 3)
, instead of (2, 3)
as one might expect:
select id from datetime_test where my_date >= '2020-12-11 17:14:07.198';
The reason is that the millisecond part of this datetime is actually stored as .197
:
-- Result: 2020-12-11 17:14:07.197
select convert(datetime, '2020-12-11 17:14:07.198');
My Question
I am working with existing c# code that uses SQL to compare datetimes using >=
. Something like this:
public Foo GetMyColumn(DateTime inputDatetime)
{
// ...
}
select my_column
from my_table
where my_datetime >= @inputDatetime
I am trying to reuse this c# method to perform an exclusive comparison...the equivalent of using >
. How can I do that?
My original attempt was to add a single millisecond to the datetime input (in c#), but that will not work due to the precision issue outlined above. I suppose I could add 3 milliseconds. That feels like a hack. But would it work reliably?
Note: please assume I cannot change this SQL or method.