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/.