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 parsedTime,Date,Smalldatetime,Datetime,Datetime2OrDatetimeoffsetValue. 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
CodeAs 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 = ' 2011-01-01 '
4 Set @ Enddatetime = ' 2011-07-10 '
5 Select datediff (QQ, @ startdatetime, @ enddatetime)
2. Search for orders in the last three months.
The Code is as follows:
View codeDeclare @ startdatetime
Declare @ enddatetime
Set@ Startdatetime='2011-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.
For details, refer to technet.
: Datediff