How to query the records of this week and this month in SQL Server [Change and Record]

Source: Internet
Author: User

==================================
Table Name: tableName
Time Field name: theDate
==================================
DatePart Functions

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 the current day
Select * from tableName where DATEPART (dd, theDate) = DATEPART (dd, GETDATE () and 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 records of this month
Select * from tableName where DATEPART (mm, theDate) = DATEPART (mm, GETDATE () and DATEPART (yy, theDate) = DATEPART (yy, GETDATE ())

Query this season's records
Select * from tableName where DATEPART (qq, theDate) = DATEPART (qq, GETDATE () and DATEPART (yy, theDate) = DATEPART (yy, GETDATE ())

Query records of the current year
Select * from tableName where DATEPART (yy, theDate) = DATEPART (yy, GETDATE ())

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

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------
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 records of the current day
Select count (*) from tableName where (DATEDIFF (dd, theDate, GETDATE () = 0)
Query records of this week
Select count (*) from tableName where (DATEDIFF (wk, theDate, GETDATE () = 0)
Query records of this month
Select count (*) from tableName where (DATEDIFF (mm, theDate, GETDATE () = 0)
Query this season's records
Select count (*) from tableName where (DATEDIFF (qq, theDate, GETDATE () = 0)
Query records of the current year
Select count (*) from tableName where (DATEDIFF (yy, theDate, GETDATE () = 0)

Article: http://blog.csdn.net/coolwzjcool/archive/2007/08/25/1758470.aspx

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.