Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2003
    Posts
    3

    Unanswered: SELECT within a date range

    Hi, I'm relatively new to mySQL, and am trying to write a query that will select all entries in a table that are between 2 dates. For example a table like this:
    Code:
    |---------------|
    |name|date      |
    |---------------|
    |Alan|2003-02-02|
    |Ben |2003-01-30|
    |Paul|2004-02-25|
    Is there a simple query that will allow me to select all the names that fall between 2 different dates (say 2003-01-01 and 2003-03-01)

    Thanks in advance for any replies

  2. #2
    Join Date
    Feb 2003
    Location
    Nuremberg, Germany
    Posts
    12

    Re: SELECT within a date range

    Originally posted by toolkit
    Hi, I'm relatively new to mySQL, and am trying to write a query that will select all entries in a table that are between 2 dates. For example a table like this:
    Code:
    |---------------|
    |name|date      |
    |---------------|
    |Alan|2003-02-02|
    |Ben |2003-01-30|
    |Paul|2004-02-25|
    Is there a simple query that will allow me to select all the names that fall between 2 different dates (say 2003-01-01 and 2003-03-01)

    Thanks in advance for any replies
    You can use the same comparison operators like on other data types, e.g.

    Code:
    SELECT name FROM mytable WHERE 'date' > '2003-01-01' AND 'date' < '2003-03-01';
    or, even more easy:
    Code:
    SELECT name FROM mytable WHERE 'date' BETWEEN '2003-01-01' AND '2003-03-01';
    note1: pay attention when you called a column 'date'. it needs to be quoted since it is a sql keyword. better avoid such names at all.

    note2: pay attention when your column is of type datetime or timestamp instead of simple date, e.g.
    '2003-03-01 04:15:55' will NOT be found with the restriction examples above because when the time part of a datetime is missing, always '00:00:00' is assumed.

  3. #3
    Join Date
    Jan 2003
    Posts
    3
    Thanks frankn, so it's that easy? I love mysql

Posting Permissions

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