SQL Server date Query-sql query today, yesterday, 7 days, 30 days

Source: Internet
Author: User



Records reprinted from: Http://www.cnblogs.com/suruozhong/p/5974595.html



All data today: select * from table name where DateDiff (dd, datetime type field, getdate ()) = 0

All data from yesterday: select * from table name where DateDiff (dd, datetime type field, getdate ()) = 1

All data within 7 days: select * from table name where DateDiff (dd, datetime field, getdate ()) <= 7

All data within 30 days: select * from table name where DateDiff (dd, datetime type field, getdate ()) <= 30

All data for this month: select * from table name where DateDiff (mm, datetime type field, getdate ()) = 0

All data for this year: select * from table name where DateDiff (yy, datetime field, getdate ()) = 0

 

 

Query today is the day of the year: select datepart (dayofyear, getDate ())

Query today is the day of the month: 1. select datepart (dd, getDate ())

                        2.select day (getDate ())

Query the Monday date of this week (Note: The specified date cannot be Sunday. If it is Sunday, it will be calculated to the next Monday. So if it is Sunday, one day is subtracted.) getdate ()), 0)

 

Query yesterday's date: select convert (char, dateadd (DD, -1, getdate ()), 111) // 111 is the style number, (100-114)

 

Query the date of the first day of the month: Select DATEADD (mm, DATEDIFF (mm, 0, getdate ()), 0) as firstday

Query the date of the last day of the month: Select dateadd (ms, -3, DATEADD (mm, DATEDIFF (m, 0, getdate ()) + 1, 0)) as lastday // The value of -3 will change accordingly

 

How many days in the month: select datepart (dd, dateadd (dd, -1, dateadd (mm, 1, cast ((cast (year (getdate ()) as varchar) + '-' + cast (month (getdate () ) as varchar) + '-01') as datetime))))

 

Find the difference between the two time periods by a few days: select datediff (day, ‘2012/8/1’, ‘2012/8/20’) as daysum

± N days on the specified date: select convert (char, dateadd (dd, 1, ‘2012/8/20‘), 111) as riqi // output 2012/8/21

± N minutes on the specified date: select dateadd (mi, -15, getdate ()) // Query the date 15 minutes before the current time?




SQL Server date Query-sql query today, yesterday, 7 days, 30 days


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.