Date calculation

  • I am trying to build a query that will run at various times, but I want to use two date settings (beginning and ending) for selection purposes. Regardless of the day the process is initiated I would like to build the "From Date" to be equal to the prior month of the prior year on the 15th day, and the "To Date" to be the prior month of the current year on the 15th day. Both dates to be at midnight.

    Example:

    Process initiated on 6/24/2009.

    Desired "From Date" = 5/15/2008

    Desired "To Date" = 5/15/2009

    This is intended to give me from Midnight 5/15/2008 through the end of 5/14/2009.

    Any suggestions would be appreciated.

    Thanks in advance.

  • How's this:

    /*

    Process initiated on 6/24/2009.

    Desired "From Date" = 5/15/2008

    Desired "To Date" = 5/15/2009

    */

    declare @ProcessDate datetime;

    set @ProcessDate = '2009-06-24'

    select

    dateadd(dd, 14,dateadd(yy, -1, dateadd(mm,datediff(mm,0,@ProcessDate) - 1,0))),

    dateadd(dd, 14, dateadd(mm,datediff(mm,0,@ProcessDate) - 1,0))

  • Perfect! Thank you very much.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply