Results 1 to 6 of 6

Thread: Query for finding records between to date

  1. #1
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default Query for finding records between to date

    I can't get my head around this one. I have a database table with all events that are held in a city. On the website events are shown for each month. Al events have start date. If the event is held longer then one day there also will be a end date. In a few occasions though some events will start in the previous month and end in the next month.

    For example start August 31 and end October 4th. Because the events is running trough out September it needs to be shown on the event page for September. But I am struggling to write the appropriate query to do this.

    Does anyone have any pointers?

  2. #2
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    How about:

    Code:
    SELECT * FROM events WHERE (start_date >= [01/MONTH/YEAR] AND end_date <= [31/MONTH/YEAR]) OR (start_date <= [01/MONTH/YEAR] AND end_date <= [31/MONTH/YEAR]) OR (start_date >= [01/MONTH/YEAR] AND end_date >= [31/MONTH/YEAR])
    (all events will need an end date, even if it is the same as the start date)
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  3. #3
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Just updated that query, not sure if it's right though - bit of a puzzler!
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  4. #4
    Join Date
    Mar 2008
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    The following is a bit shorter but should work too:

    Code:
    select	*
    FROM	events
    WHERE	start_date <= [31/MONTH/YEAR]
    AND	end_date >= [01/MONTH/YEAR]
    Like Warren said, you'd need an end date on every record, even if it's the same as the start date.

    Hope this helps
    Dan

  5. #5
    Join Date
    Apr 2005
    Location
    Haslemere, Surrey, UK
    Posts
    338
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default

    Or this one should allow for your end dates being NULL (empty)



    Declare @MonthStart smalldatetime
    Declare @MonthEnd smalldatetime

    -- Set our month duration
    set @MonthStart = cast('23 May 2007' as smalldatetime)
    set @MonthEnd = cast('31 May 2007' as smalldatetime)

    SELECT * FROM EventItem
    WHERE (EventStart between @MonthStart and @MonthEnd and EventEnd IS NULL) -- NULL End Date shows its a one day event
    OR (EventEnd >= @MonthStart and EventStart <= @MonthEnd and EventEnd IS NOT NULL) -- Multiple day events

    Alastair - WOWD



  6. #6
    Join Date
    Apr 2006
    Location
    Amsterdam
    Posts
    350
    Thanks
    20
    Thanked 11 Times in 11 Posts

    Default

    Thanks for all the help guys.

    I'm using unix timestamps for the dates. So if an event only last one day the end date is will be 0.

    I went with this query:
    Code:
    SELECT * FROM  events WHERE 
    (FROM_UNIXTIME(unixdate) >= '2009-09-01' 
    AND FROM_UNIXTIME(unixenddate)  <= '2009-09-30') 
    OR
    (FROM_UNIXTIME(unixdate) <= '2009-09-01' 
    AND FROM_UNIXTIME(unixenddate)  >= '2009-09-01')

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. One query table
    By Rappie in forum MySQL
    Replies: 2
    Last Post: 17th March 2008, 11:03 PM
  2. DSN - no records returned from query
    By socket in forum MSSQL
    Replies: 4
    Last Post: 22nd October 2006, 09:00 PM
  3. XML/XSL date format
    By Rappie in forum XML
    Replies: 1
    Last Post: 27th July 2006, 01:10 PM
  4. Toublesome Date!
    By s80wkr in forum ASP (VBScript)
    Replies: 9
    Last Post: 8th November 2005, 12:21 PM

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
  •