Returns the time difference between the two dates in the SQL statement. Convert the time difference to the number of days format [reserve by yourself].

Source: Internet
Author: User
Document directory
  • Syntax
  • Example 1
  • Example 2
SQL Server datediff () function definition and usage

The datediff () function returns the number of days between two dates.

Syntax
DATEDIFF(datepart,startdate,enddate)

StartdateAndEnddateA parameter is a valid date expression.

DatepartParameters can be the following values:

Datepart Abbreviations
Year YY, yyyy
Quarter QQ, Q
Month Mm, m
Day of the year Dy, y
Day DD, d
Week Wk, WW
Week DW, W
Hours HH
Minutes Mi, n
Seconds SS, S
Millisecond MS
Subtle MCS
Nanoseconds NS
Example 1

Use the following SELECT statement:

SELECT DATEDIFF(day,'2008-12-29','2008-12-30') AS DiffDate

Result:

Diffdate
1
Example 2

Use the following SELECT statement:

SELECT DATEDIFF(day,'2008-12-30','2008-12-29') AS DiffDate

Result:

Diffdate
-1

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.