如何在任何月份和年份的选择中获得最后3个月

时间:2015-07-08 06:33:44

标签: sql-server-2008

请帮我解决一下问题:

  • 如何在任何月份的选择中获得最后3个月?

我需要在任何月份的选择中持续3个月

例如:如果我选择 7月 2015,那么我需要几个月 06,05,04

2 个答案:

答案 0 :(得分:0)

SELECT * 
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE()) 

答案 1 :(得分:0)

--set @Input_Date as 07-01-2015 for July 2015

Declare @Input_Date Date

    SELECT * 
    FROM TABLE_NAME
    WHERE cast(Date_Column as date) >= cast(DATEADD(MONTH, -3,@Input_Date) as date)   and  cast(Date_Column as date) <=@Input_Date

    --cast to date is purposely done to avoid comparision issues for time part