SQL Server: Get data for only the past year

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

    For some reason none of the results above worked for me.

    This selects the last 365 days.

     SELECT ... From ... WHERE date BETWEEN CURDATE() - INTERVAL 1 YEAR AND CURDATE()
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 17:38

    The other suggestions are good if you have "SQL only".

    However I suggest, that - if possible - you calculate the date in your program and insert it as string in the SQL query.

    At least for for big tables (i.e. several million rows, maybe combined with joins) that will give you a considerable speed improvement as the optimizer can work with that much better.

    0 讨论(0)
  • 2020-12-12 17:40

    The most readable, IMO:

    SELECT * FROM TABLE WHERE Date >
       DATEADD(yy, -1, CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)))
    

    Which:

    1. Gets now's datetime GETDATE() = #8/27/2008 10:23am#
    2. Converts to a string with format 101 CONVERT(varchar, #8/27/2008 10:23am#, 101) = '8/27/2007'
    3. Converts to a datetime CONVERT(datetime, '8/27/2007') = #8/27/2008 12:00AM#
    4. Subtracts 1 year DATEADD(yy, -1, #8/27/2008 12:00AM#) = #8/27/2007 12:00AM#

    There's variants with DATEDIFF and DATEADD to get you midnight of today, but they tend to be rather obtuse (though slightly better on performance - not that you'd notice compared to the reads required to fetch the data).

    0 讨论(0)
  • 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())
    
    0 讨论(0)
  • 2020-12-12 17:43

    argument for DATEADD function :

    DATEADD (*datepart* , *number* , *date* )
    

    datepart can be: yy, qq, mm, dy, dd, wk, dw, hh, mi, ss, ms

    number is an expression that can be resolved to an int that is added to a datepart of date

    date is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value.

    0 讨论(0)
提交回复
热议问题