SQL datediff calculation time difference

Source: Internet
Author: User

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

 

 

 

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.