What I am trying to do is take a date in SQL Server and find the last possible hour, minute, second, and millisecond of that date.
So if the date is this: 2021-02-16 13:08:58.620
I would like to return: 2021-02-16 23:59:59.999
I have tried something like this but it is not working and I guess that I am missing something where it is converting the time and keeping the hour, minute, second and millisecond of that date
Select
DateAdd(MILLISECOND, -1, (Convert(datetime, DATEADD(day, 1, DateValue), 101))) as lastPossibleDate
From
Table1
DATETIME
datatype in SQL Server has an accuracy of only 3.33ms - so23:59:59.999
will be rounded up to the next day - the max value that you can represent as aDATETIME
is23:59:59.997
. Best solution would be to useDATETIME2(n)
datatype instead which has an accuracy of as little as 100 ns ..... – Masurium<= end of the day
, don't..... Instead use< next day
– Spill