How to obtain the maximum time value of the current system date, such as yyyy-MM-dd 23:59:59. 997.
We can use the DATEADD function.
The DATEADD () function adds or removes the specified time interval in the date.
Syntax
The DATEADD (datepart, number, date) date parameter is a valid date expression. Number is the number of intervals you want to add. For the future time, this number is a positive number, and for the past time, this number is a negative number.
We will use DATEADD to add a day to the midnight time value of the current date. That is, we will get the midnight time value of the next day, and then subtract 3 seconds, that is, you can obtain the maximum time value of the current system date.
DECLARE @ Date DATETIME = CURRENT_TIMESTAMP -- Current system Date and time
DECLARE @ midnight DATETIME
SET @ midnight = CAST (@ Date as date) as datetime)
Select dateadd (MS,-3, DATEADD (day, 1, @ midnight) -- 23:59:59. 997