SQL Server queries data based on time period

Source: Internet
Author: User

Today, a colleague found a SQL Server statement execution is particularly slow, after the inspection found that the condition plus the time period, before using the time field between XXX and xxx way, then change the time to determine the method,

----------Dafediff----------Return two points of difference, optional units YY,MM,DD,HH,SS, etc.

Query this year's data:

SELECT * from DateDiff (yy, Time field, GETDATE ()) =0

Query data for 2016 years:

SELECT * from DateDiff (yy, Time field, ' 2016 ') =0

----------Dafepart----------intercept a unit at a point in time, such as intercept year or month or day, optional unit YY,MM,DD,HH,SS, etc.

Query data for a certain period of time, such as February 2016 to December 2016:

SELECT * from DateDiff (yy, Time field, ' =0 ') and datepart (mm, Time field) >=2 and datepart (mm, Time field) <=12

SQL Server queries data based on time period

Related Article

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.