SQL Server: Get data for only the past year

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

    I found this page while looking for a solution that would help me select results from a prior calendar year. Most of the results shown above seems return items from the past 365 days, which didn't work for me.

    At the same time, it did give me enough direction to solve my needs in the following code - which I'm posting here for any others who have the same need as mine and who may come across this page in searching for a solution.

    SELECT .... FROM .... WHERE year(*your date column*) = year(DATEADD(year,-1,getdate()))
    

    Thanks to those above whose solutions helped me arrive at what I needed.

提交回复
热议问题