5

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
4
  • 2
    You'll run into problems because the DATETIME datatype in SQL Server has an accuracy of only 3.33ms - so 23:59:59.999 will be rounded up to the next day - the max value that you can represent as a DATETIME is 23:59:59.997. Best solution would be to use DATETIME2(n) datatype instead which has an accuracy of as little as 100 ns .....
    – marc_s
    Commented Jun 7, 2021 at 20:36
  • 3
    Just use LESS THAN and midnight the next day...
    – Caius Jard
    Commented Jun 7, 2021 at 20:36
  • @marc_s thanks for the comment never knew about the DATETIME2(n) very good information
    – Eric
    Commented Jun 7, 2021 at 20:42
  • 3
    Please explain why you want this. If this is so you can use <= end of the day, don't..... Instead use < next day
    – Nick.Mc
    Commented Jun 8, 2021 at 4:06

4 Answers 4

7

Turn it into a date to truncate the time, add a day, turn it back to a datetime, subtract a millisecond...

declare @Test datetime2(3) = '2021-02-16 13:08:58.620';

select dateadd(millisecond, -1, convert(datetime2(3),dateadd(day, 1, convert(date, @Test))));
Result
2021-02-16 23:59:59.999

Note if you use straight datetime rather than datetime(2) your accuracy is only down to 3ms. datetime(2) is the recommended datetime datatype to use.

1
  • Worked Like a charm thanks never knew of the datetime2 until you and @marc_s mentioned it
    – Eric
    Commented Jun 7, 2021 at 20:41
2

Another simply way would be to cast your datetime to a date and just concatenate it with the maximum possibly time for your given precision. If you want accuracy to 3 decimal places you need to use datetime2(3)

declare @date datetime='20210216 13:08:58.620';

select Convert(datetime2(3),Concat(convert(date,@date),' 23:59:59.999'));
2

Just another option via format()

declare @Test datetime2(3) = '2021-02-16 13:08:58.620';

select convert(datetime2(3),format(@Test,'yyyy-MM-dd 23:59:59.999'))
2
  • Hmm, I'm not sure I would use string parsy anything for date situations that could be done numerically..
    – Caius Jard
    Commented Jun 8, 2021 at 6:17
  • 1
    @CaiusJard It is just an option.... another path from A to B... another tool in the belt... Depending on the use case, I may not use this either. It is 2 functions vs. 4, and the outer may be optional. Besides, sometimes you just need a hammer. It may not be pretty or sophisticated, but it gets the job done :) Commented Jun 8, 2021 at 13:22
2

For datetime datatype, the maximum value possible for time is : 23:59:59.997

Datetime from MSDN

We can use DateTimeFromParts to derive maximum possible datetime value for the day.

DECLARE @datevalue datetime = '2021-02-16 13:08:58.620'

SELECT DATETIMEFROMPARTS ( year(@datevalue), month(@datevalue), day(@datevalue), 23, 59, 59, 997 )  as maxPossibleValueForDay 
maxPossibleValueForDay
2021-02-16 23:59:59.997

Not the answer you're looking for? Browse other questions tagged or ask your own question.