SQL Server: Get data for only the past year

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

    Well, I think something is missing here. User wants to get data from the last year and not from the last 365 days. There is a huge diference. In my opinion, data from the last year is every data from 2007 (if I am in 2008 now). So the right answer would be:

    SELECT ... FROM ... WHERE YEAR(DATE) = YEAR(GETDATE()) - 1
    

    Then if you want to restrict this query, you can add some other filter, but always searching in the last year.

    SELECT ... FROM ... WHERE YEAR(DATE) = YEAR(GETDATE()) - 1 AND DATE > '05/05/2007'
    

提交回复
热议问题