SQL to obtain the maximum time value of the current date

Source: Internet
Author: User
Tags datetime time interval

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.