SQL Server: Get data for only the past year

后端 未结 12 836
不思量自难忘°
不思量自难忘° 2020-12-12 17:09

I am writing a query in which I have to get the data for only the last year. What is the best way to do this?

SELECT ... FROM ... WHERE date > \'8/27/2007         


        
12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 17:40

    I had a similar problem but the previous coder only provided the date in mm-yyyy format. My solution is simple but might prove helpful to some (I also wanted to be sure beginning and ending spaces were eliminated):

    SELECT ... FROM ....WHERE 
    CONVERT(datetime,REPLACE(LEFT(LTRIM([MoYr]),2),'-
    ','')+'/01/'+RIGHT(RTRIM([MoYr]),4)) >=  DATEADD(year,-1,GETDATE())
    

提交回复
热议问题