In the development of database applications, processing time problems are often encountered, such as querying records at a specified time. Next we will discuss these common problems with your own experience.
First, we will introduce the usage of several major functions for processing time in SQL Server:
Getdate () function: Gets the current date and time of the system. The return value is of the datetime type.
Usage: getdate ()
Example:
Select getdate () as dte, dateadd (day,-1, getdate () as nowdat
Output result:
Dte nowdat
------------------------------------------------------
19:13:10. 083 19:13:10. 083
(1 row (s) affected)
Datepart () function: returns the specified part of the time in integer format.
Usage: datepart (datepart, date)
Parameter description: the part of the time to be returned for datepart. Values include year, month, day, hour, and minute.
Date is the specified time.
Example:
Select datepart (month, GETDATE () AS 'month number'
Output result:
Month Number
------------
11
(1 row (s) affected)
Dateadd () function: adds an integer to the specified part of the specified time to return a new time value.
Usage: dateadd (datepart, number, date)
Parameter description: datepart (same as above)
Date (same as above)
The value to be added by number. It is an integer that can be positive or negative. A positive value returns the time value after date, and a negative value returns the date.
Previous time value
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