Use of DateDiff in SQL

Source: Internet
Author: User

Introduction: We often in SQL to determine whether the year or month or the day is equal, we can use the DateDiff function, easy

DateDiff: Determine the number of differences in year or month or day or week, week, hour, minute, etc.
Use format:

DATEDIFF (Datepart,startdate,enddate)

Day (difference in days): can also be replaced with the following code

DATEDIFF (Day, ' 2008-12-29 ', ' 2008-12-30 ') as diffdate       -result 1SELECT DATEDIFF (day, ' 2008-12-30 ', ' 2008-12-29 ') as Diffdate-  The result is-1

Month (Month difference): You can also use the following code to replace

SELECT DATEDIFF (MM, ' 2008-12-30 ', ' 2010-12-29 ') as Diffdate  -the result is 24

Year (difference in years): You can also use the following code to replace

SELECT DATEDIFF (year, ' 2008-12-30 ', ' 2010-12-29 ') as diffdate  -result 2SELECT DATEDIFF (yy, ' 2008-12-30 ', ' 2010-12-29 ') ) as Diffdate    --Results of 2

If you think reading this blog gives you something to gain, you might want to click " recommend " in the lower right corner.

If you want to find my new blog more easily, click on " Follow me "

If you want to give me more encouragement, you may wish to click on the right, " enjoy your stay"

Blog is his own summary of the results of learning, learning to summarize the knowledge-"analysis of the problem-" to solve the problem.

Where the views/descriptions in the text are incorrect, please correct them.

Thank you for your reading, if you are interested in the content of my blog, please continue to follow my following blog, I am yxtic.

Use of DateDiff in SQL

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.