none
Retrieve last 12 months data RRS feed

  • Question

  • I have a data field in my table that i would to use within my WHERE clause, to get the last 12 months dataset

    so for example, WHERE date between '20110323' and '20100323'

    Is this possible? I know I have to use something like GETDATE() or something but not quite sure how to incorporate this.

    Thursday, March 24, 2011 9:27 AM

Answers

  • SELECT * FROM tbl WHERE dt >=DATEADD(month,-12,DATEADD(day,DATEDIFF(day,0,GETDATE()),0)) AND dt <=DATEADD(day,DATEDIFF(day,0,GETDATE()),0)

     

     


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    • Marked as answer by Sam233 Thursday, March 24, 2011 9:38 AM
    Thursday, March 24, 2011 9:35 AM
    Answerer

All replies