Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2006
    Posts
    87

    Unanswered: help on getting a range from dates

    Hi everybody..

    Need help on this situation..
    I need to extract results of a query basing on the selected range of dates by the user ....

    selection
    date1 = 01/01/2009
    date2 =03/31/2009

    the table for this query is:

    dateid (int)
    startdate (datetime)
    endDate (datetime)

    how could i refer this into a query where date1 = datetime and date2=enddate

    I tried using the query With a where clause "startdate between date1 and date2" but it ishould be between the startdate and enddate

    thanks

  2. #2
    Join Date
    Dec 2008
    Posts
    135
    declare @date1 datetime,@date2 datetime
    select @date1 = '01/01/2009',@date2 ='03/31/2009'

    select * from urtable where dateadd(d,0,datediff(d,0,startdate)) > dateadd(d,0,datediff(d,0,@date1)) AND dateadd(d,0,datediff(d,0,endate)) < dateadd(d,0,datediff(d,0,@date2))

  3. #3
    Join Date
    Aug 2006
    Posts
    87

    Thumbs up

    Quote Originally Posted by bklr
    declare @date1 datetime,@date2 datetime
    select @date1 = '01/01/2009',@date2 ='03/31/2009'

    select * from urtable where dateadd(d,0,datediff(d,0,startdate)) > dateadd(d,0,datediff(d,0,@date1)) AND dateadd(d,0,datediff(d,0,endate)) < dateadd(d,0,datediff(d,0,@date2))

    thanks this got my problem right again thanks

  4. #4
    Join Date
    Dec 2008
    Posts
    135
    ur welcome

  5. #5
    Join Date
    Aug 2006
    Posts
    87
    HI again the solution of bklr was doing fine until i tried a range with the same month example @date1 ='01/01/2009' and @date2='31/01/2009' the problem with this range is it doesn't include records that starts on january but ends on the succeeding months unless i specify the @date2 ='02/31/2009' etc... how could i modify the code so that the record will show

    again thanks
    alexiop

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    perhaps this might help --
    http://www.dbforums.com/microsoft-sq...ate-range.html

    see post #14

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  7. #7
    Join Date
    Aug 2006
    Posts
    87
    thanks that made the query right with all the explanations it helped me a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •