SQL query today, yesterday, 7 days, 30 days of data

All data today: the SELECT  *  from table name the WHERE  DateDiff (dd, datetime type field, getdate ()) = 0 

for all data yesterday: the SELECT  *  from table name the WHERE  DateDiff (dd, datetime type field, getdate ()) = 1 

All data 7 days: SELECT  *  from table WHERE  the DateDiff (dd, datetime type field, getdate ()) <= 7 

30 days all the data: SELECT  *  from table WHERE  the DateDiff (dd, datetime type field, getdate()) <= 30 

all data month: SELECT  *  from table WHERE  the DateDiff (mm, datetime type field, getdate ()) = 0 

for all data of the current year: SELECT  *  from table WHERE  the DateDiff (YY, type datetime field, getdate ()) = 0

 

Today is the day of inquiry this year: the SELECT  the datepart of a (DAYOFYEAR, getDate ()) 

query today is the day of the month: 1 . The SELECT  the datepart of a (dd, getDate ())   

                                                2 . The SELECT  Day ( getDate ()) 

query this week Monday date is much (note: not specified date is Sunday, if Sunday will be calculated until Monday to Sunday so if it is to cut one day) 
the SELECT  DATEADD (wk, DATEDIFF (wk, 0 , getdate ( )), 0 ) 

 

inquiry yesterday date: the SELECT  Convert For ( char , DATEADD (DD, - 1 , getdate()), 111 )   // 111 is the style number, ( 100 - 114 ) 

 

queries the Month Day Date: the Select  the DATEADD (mm, the DATEDIFF (mm, 0 , getdate ()), 0 ) AS the FirstDay 

query last month date from: the Select  DATEADD (MS, - 3 , the DATEADD (mm, the DATEDIFF (m, 0 , getdate ()) + . 1 , 0 )) AS LASTDAY       // modify - value of 3 will have a corresponding change in

 

How many days the month: SELECT  the datepart (dd, DATEADD (dd, - . 1 , DATEADD (mm, . 1 , Cast (( Cast ( year ( getdate ()) AS  VARCHAR ) + ' - ' + Cast ( month The ( getdate () ) AS  VARCHAR ) + ' -01 ' ) AS  datetime )))) 

 

required two days time difference: SELECT  DATEDIFF ( Day ,' 2012/8/1 ' , ' 2012/8/20 ' ) AS daysum 

± day N on the specified date: SELECT  Convert ( char , DATEADD (dd, . 1 , ' 2012/8/20 ' ), 111 ) AS riqi     // output 2012 / . 8 / 21 is 

± N on the specified date minutes: SELECT  DATEADD (mi the, - 15 , getdate ())   // check the current date and time 15 minutes before

 

Guess you like

Origin www.cnblogs.com/yu-shang/p/11596616.html