SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    England
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search for rows between start and end dates

    Hi,

    I'm in the middle planning a simple holiday home booking system, and have been working out what query's are needed, I've worked out most of them but cant seem to work this one out.

    I have got a simple table which looks likes:

    ID| start | end | price
    1 | 2010-04-10 | 2010-04-17 | 20.00
    2 | 2010-04-17 | 2010-04-24 | 25.00
    3 | 2010-04-24 | 2010-05-08 | 10.00

    I'm looking for a query which returns all the rows/records which fulls between a search criteria, for example:
    start date: 2010-04-15
    end date: 2010-04-20
    Would return row 1 and 2

    or

    start date: 2010-04-24
    end date: 2010-04-30
    Would return row 3

    Any help would be much appreciated

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,014
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    you might need to explain that a bit more

    in your first example, the given range overlap rows 1 and 2, but only partially

    but your second example, doesn't the given range partially overlap row 2?
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,
    Can you please check if the following helps you to solve your problem

    Code SQL:
    DECLARE @START datetime, @END datetime
    SELECT @START = '2010-04-24', @END = '2010-04-30'
    SELECT * FROM testtable 
    WHERE 
    (START < @START AND @START < [END]) OR
    (START < @END AND @END < [END])


Bookmarks

Posting Permissions

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