SQL Statistics Common SQL

Source: Internet
Author: User

Statistics for commonly used SQL

To count commonly used SQL statements:

All data for Today:SELECT *From table namewhere DateDiff (dd,datetime Type field, GETDATE ()) =0All data for yesterday:SELECT *From table namewhere DateDiff (dd,datetime Type field, GETDATE ()) =1All data in 7 days:SELECT *From table namewhere DateDiff (dd,datetime Type field, GETDATE ()) <=7All data in 30 days:SELECT *From table namewhere DateDiff (dd,datetime Type field, GETDATE ()) <=30All data for this month:SELECT *From table namewhere DateDiff (mm,datetime Type field, GETDATE ()) =0All data for this year:SELECT *From table namewhere DateDiff (yy,datetime Type field, GETDATE ()) =0Query today is the first day of this year:SelectDatePart (dayofyear,getdate ()) query today is the day of the month:1.SelectDATEPART (DD, getDate ())2.SelectDay (GetDate ()) Query Week 1st of the week is how much (note: The specified date cannot be Sunday, if it is Sunday will be calculated to the next Monday go.) So if it's Sunday to lose a day) SELECT DATEADD (Wk,datediff (wk,0,getdate ()),0) query yesterday's date:Select CONVERT (Char,dateadd (dd,-1,getdate ()),111)//111 is the style number, (100-114)Query first day of the month Date: Select DATEADD (mm, DATEDIFF (mm,0,getdate ()),0)AsFirstDay query last day of the month Date: Select DateAdd (ms,-3,dateadd (mm, DATEDIFF (M,0,getdate ()) +1,0))As Lastday//Change-the value of 3 changes accordinglyHow many days are there this month:Select DATEPART (Dd,dateadd (dd,-1,dateadd (MM,1,cast ((CAST (year (GETDATE ())As varchar) +‘-' +cast (Month (getdate ())As varchar) +‘-01‘ )As(datetime))), two time periods of difference:Select DateDiff (Day, ' 2012/8/1 ' 2012/8/20 "), Span style= "color: #800080;" >111) as Riqi // output 2012/ 8/21 ±n minutes on the specified date: select DateAdd (Mi,-15,getdate ()) " Span style= "color: #008000;" >// Query the date 15 minutes before the current time?           

SQL Statistics Common SQL

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.