SQL Server: Get data for only the past year

后端 未结 12 872
不思量自难忘°
不思量自难忘° 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:18

    declare @iMonth int
    declare @sYear varchar(4)
    declare @sMonth varchar(2)
    set @iMonth = 0
    while @iMonth > -12
    begin
        set @sYear = year(DATEADD(month,@iMonth,GETDATE()))
        set @sMonth = right('0'+cast(month(DATEADD(month,@iMonth,GETDATE())) as varchar(2)),2)
        select @sYear + @sMonth
        set @iMonth = @iMonth - 1
    end
    

提交回复
热议问题