SQL Server queries this week's current month, this year's record statistics this month, this week's statistics and this quarter

Source: Internet
Author: User

Table Name: tablename time field name: thedate ============================== datepart Function

Date Abbreviations
Year YY, yyyy
Quarter QQ, Q
Month Mm, m
Dayofyear Dy, y
Day DD, d
Week Wk, WW
Weekday DW
Hour HH
Minute Mi, n
Second SS, S
Millisecond MS

Query records of this month. Select * From tablename where datepart (mm, thedate) = datepart (mm, getdate () and datepart (YY, thedate) = datepart (YY, getdate ())
Query records of this week. Select * From tablename where datepart (wk, thedate) = datepart (wk, getdate () and datepart (YY, thedate) = datepart (YY, getdate ())
Query this season's record select * From tablename where datepart (QQ, thedate) = datepart (QQ, getdate () and datepart (YY, thedate) = datepart (YY, getdate ())
Specifically, getdate () is a function used to obtain the system time.

 

Functions ------------------- datediff Function

Date 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

Query the current day's record select count (*) from tablename where (datediff (DD, thedate, getdate () = 0) query this month's record select count (*) from tablename where (datediff (mm, thedate, getdate () = 0) query this year's record select count (*) from tablename where (datediff (YY, thedate, getdate ()) = 0)

 

Source: http://www.bcbbs.net/news/Content.aspx? Id = 34330 Article Source: http://www.bcbbs.net/news/Content.aspx? Id = 34330

 

 

Table Name: tablename time field name: thedate

Query records of this month. Select * From tablename where datepart (mm, thedate) = datepart (mm, getdate () and datepart (YY, thedate) = datepart (YY, getdate ())

Query records of this week. Select * From tablename where datepart (wk, thedate) = datepart (wk, getdate () and datepart (YY, thedate) = datepart (YY, getdate ())

Query this season's record select * From tablename where datepart (QQ, thedate) = datepart (QQ, getdate () and datepart (YY, thedate) = datepart (YY, getdate ())

Specifically, getdate () is a function used to obtain the system time.

 

 

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.