Thread: BETWEEN date

    #1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    BETWEEN date


    Hello;

    In order to get all ids between these 2 days, which one is prefered and why?

    Code:
    SELECT id WHERE date_added BETWEEN '2012-12-12' AND '2012-12-16'
    Code:
    SELECT id WHERE date_added >= '2012-12-12' AND date_added <= '2012-12-16'
    Thank you.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,960
    Rep Power
    9397
    Same difference. First is easier to read, second may be easier to implement in code (like if you might not always need both endpoints).
  4. #3
  5. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,118
    Rep Power
    4274
    Originally Posted by zxcvbnm
    ...which one is prefered and why?
    depends, as requinix said, if you want to include the endpoints

    this difference does not apply if date_added is a DATE column, but consider what happens if it's a DATETIME column

    the intent of the query is to return all rows on the 16th? or all rows up to but not including the 16th?

    see Simply SQL, page 86
    Last edited by r937; April 2nd, 2013 at 12:04 AM.
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

IMN logo majestic logo threadwatch logo seochat tools logo