Date stuff

  • I need some help with a date. 

    Here is the scenaro:

    I have a stored proceedure that will have 1 variable (datetime)

    The select statement works fine.  What I need to figure out is how to contrain the data based on 2 things

    1. The variable information for instance 01/15/2007 and

    2  The month that day falls in

    so the end result need to be select all record with the criteria of @startdate and (the month that falls in) 

    Any help would be greatly appreciated. 

  • I'm just a little confused, what is the criteria for select records again?  My guess is you are looking for all records in the current month based on the value in @startdate, for instance: @startdate = '2007-07-18', all records in '2007-07-%'; correct?

     

  • That is correct

  • Try this:

    where

    datefield >= dateadd(mm, datediff(mm,0,@startdate), 0) and datefield < dateadd(mm, 1, dateadd(mm, datediff(mm,0,@startdate), 0))

  • declare @StartDate datetime
    set @StartDate = '20070718'
    Select
     *
    from
     MyTable
    where
     -- Greater than or equal start of month
     MyDate >= dateadd(mm,datediff(mm,0,@StartDate),0) and
     -- Less than start of next month
    MyDate < dateadd(mm,datediff(mm,0,@StartDate)+1,0)
    
  • Michael, I knew there had to be another way to get the upper bound for the where clause.

     

  • Thank you very much Lynn and Michael.  It worked perfectly. Kudo's to both

Viewing 7 posts - 1 through 6 (of 6 total)

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