-1

T-sql SmallDateTime add day when time passed 23:30

declare @sampledate DateTime
set @sampledate='2020-09-18 23:59:53'
select  @sampledate AS Date,
       CAST(@sampledate as DATE) AS CAST_Date,
       CAST(@sampledate as SmallDateTime) CAST_smalldatetime,
   CONVERT(SmallDateTime,@sampledate) Convert_smalldatetime
jarlh
  • 42,561
  • 8
  • 45
  • 63
Chetan
  • 1
  • 2
    What does `23:30` have to do with your code sample? What, precisely, is your question? – HABO Sep 22 '20 at 03:47

3 Answers3

1

When converting datetime to smalldatetime, SQL will round the minute based on the seconds. It works out that 23:59:29.998 and below will round down to 23:59, and 23:59:29.999 and up will round up to tomorrow.

But does this answer your question?

Philip Kelley
  • 39,426
  • 11
  • 57
  • 92
0

How about adding 30 minutes and converting to a date?

convert(date, dateadd(minute, 30, @sampledate)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

This is the expected behaviour. Look at Property Time range from MS Docs.

2007-05-09 23:59:59 will round to 2007-05-10 00:00:00

luisvenezian
  • 441
  • 5
  • 18