Comparison of time in SQL Server Plus and minus time

Source: Internet
Author: User

Recently in the writing of a project, using the time of comparison and time of the addition and subtraction, here briefly, I was just contact with the database soon, if there are said improper place to hope that you forgive, correct me

A comparison of time

In the beginning, I spent more time with time>getdate () simple rough

In fact, SQL Server has its own time comparison function, so it can be brought with it, but also safe and convenient

DATEDIFF ();

The parsing of the DateDiff function in the W3school I refer to IS as follows:

Grammar
   DATEDIFF (datepart,startdate,enddate)
Which datepart is only you want to compare this two-time year? Or a month? Or is it a day? What's the difference between hours?
The specific wording of the reference:
DatePart Abbreviations
Years YY, yyyy
Quarter QQ, Q
Month MM, M
The day of the year Dy, y
Day DD, D
Week WK, WW
Week DW, W
Hours hh
Minutes MI, n
Seconds SS, S
Milliseconds Ms
Subtle MCs
Na-Sec Ns

Simply write an example to illustrate the following:

DATEDIFF (HH, ' 2014-06-25 8:00:00 ', ' 2014-06-25 12:58:00 ') gets a value of 4, which is the time difference between two hours compared to 4 hours

If you want to compare the number of days, change the HH to D, other comparisons can be seen

Second, the time of the addition and subtraction

I started with the time+1 is in time to add a day, minus one day with Time-1, the same simple rough

Let's talk about the functions of time-plus-minus in SQL Server

The syntax is as follows:

     DATEADD (datepart,number,date)
Which datepart is the form of time you want to add and subtract, plus year? Or a month? Or the day, etc.
The specific wording is as follows:
DatePart Abbreviations
Years YY, yyyy
Quarter QQ, Q
Month MM, M
The day of the year Dy, y
Day DD, D
Week WK, WW
Week DW, W
Hours hh
Minutes MI, n
Seconds SS, S
Milliseconds Ms
Subtle MCs
Na-Sec Ns

The feeling is similar to the above DateDiff, see the example directly:

DATEADD (D,1,getdate ()) is the current time plus day, minus one day with DATEADD (D,-1,getdate ())

If it is for the year, month, hour and so on plus minus reference, modify datepart can, convenient and quick

Ps:w3school is still a good http://www.w3school.com.cn/sql/.

Related Article

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.