SQL query day, week, one month statement of data

Source: Internet
Author: User
Tags getdate

--QueryDay:
SELECT * FROM info whereDateDiff (dd,datetime,getdate()) =0 

--Query within 24 hours :  
Select * from info where datediff (Hh,datetime,getdate ()) <=24  

-- Query this week 's records
SELECT * from info where datediff (week,datetime,getdate()) =0

-- query this month record
select * from info where datediff (Month,datetime,getdate ()) =0

--info is the table name, DateTime is the field value in the database

DATEDIFF Function:

Grammar:


DATEDIFF (DatePart, StartDate, EndDate)

Note: EndDate minus startdate. If StartDate is later than EndDate, a negative value is returned.

If the result is outside the integer value range, DATEDIFF will produce an error. For milliseconds, the maximum number is 24 days and 20 hours 31分钟零23.647秒. For seconds, the maximum number is 68 years.

Methods that are computed across boundaries such as minutes, seconds, and milliseconds make the result specified by DATEDIFF consistent across all data types. The result is a signed integer value that is equal to the number of datepart boundaries across the first and second days. For example, the number of weeks between January 4 (Sunday) and January 11 (Sunday) is 1.

SQL query day, week, one month statement of data

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.