How can I get the last 12 months from the current date PLUS extra days till 1st of the last month retrieved

后端 未结 3 1868
名媛妹妹
名媛妹妹 2021-01-18 03:48

Getting the last 12 months from a specific date is easy and can be retrieved by the following command in SQL-server. Its answer is 2014-08-17.

select Dateadd         


        
3条回答
  •  失恋的感觉
    2021-01-18 04:27

    SELECT dateadd(month,datediff(month,0,getdate())-12,0)
    

    Result is

    -----------------------
    2014-08-01 00:00:00.000
    

    So the where clause should be

    WHERE datecol >=dateadd(month,datediff(month,0,getdate())-12,0)
    

    to get all data starting from jan 01 of last year's same month

提交回复
热议问题