For information about datediff, see:
DATEDIFF (datepart ,startdate ,enddate )
-
Datepart
-
Is part of startdate and enddate of the specified cross-border type. The following table lists all valid datepart parameters. The user-defined equivalent variable is invalid.
Datepart |
Abbreviations |
Year |
YY, yyyy |
Quarter |
QQ, Q |
Month |
Mm, m |
Dayofyear |
Dy, y |
Day |
DD, d |
Week |
Wk, WW |
Hour |
HH |
Minute |
Mi, n |
Second |
SS, S |
Millisecond |
MS |
Microsecond |
MCS |
Nanosecond |
NS |
-
Startdate
-
Is an expression that can be parsed to time, date, smalldatetime, datetime, datetime2, or datetimeoffset values. Date can be an expression, column expression, user-defined variable, or string text. Subtract startdate from enddate.
To avoid uncertainty, use a four-digit year. For information about the two-digit year, see the two digit year cutoff option.
-
Enddate
-
See startdate.
1. Return two-quarter time difference records
The Code is as follows: if necessary, you can change the time field to the corresponding field in the database.
View code
1 declare @ startdatetime
2 declare @ enddatetime
3 set @ startdatetime = '2017-01-01'
4 set @ enddatetime = '2017-07-10'
5 select datediff (QQ, @ startdatetime, @ enddatetime)
2. Search for orders in the last three months.
The Code is as follows:
View code
Declare @ startdatetime
Declare @ enddatetime
Set @ startdatetime = '2017-05-01'
Set @ enddatetime = getdate ()
Select datediff (M, @ startdatetime, @ enddatetime)
3. Return the number of days between the time of the first order and the time of the last order.
Select datediff (day, (select Min (insdt) from op_order), (select max (insdt) from op_order ))
4. Use the getdate () function to obtain the current time,
If getdate () + 1 is used, the result is to add one more day in the current time.
For example:
Getdate (): 2011-08-13 13:53:09. 243
Getdate () + 1: 2011-08-14 13:53:09. 243
As shown above, add 1 directly in the period of time.