locked
Two WHERE clause together RRS feed

  • Question

  • WHERE ReferralDate BETWEEN '20080401' AND '20090430'
    AND ReferralDate BETWEEN '20090401' AND '20090430'

    How do i get the between function to work if im using two selections....? I want to select all of April in 2008/09 and all off April in 2009/10...


    is this possible?



    Tuesday, May 26, 2009 3:49 PM

Answers

  • Simply use OR:

    WHERE ReferralDate BETWEEN '20080401' AND '20080430'
    OR ReferralDate BETWEEN '20090401' AND '20090430'

    --
    Plamen Ratchev
    http://www.SQLStudio.com
    • Marked as answer by akhlaq768 Tuesday, May 26, 2009 4:00 PM
    Tuesday, May 26, 2009 3:52 PM

All replies

  • Sounds like you just want to change your AND to an OR (and fix your second date)

    WHERE ReferralDate BETWEEN '20080401' AND '20080430'
    OR ReferralDate BETWEEN '20090401' AND '20090430'



    Michael Asher
    Tuesday, May 26, 2009 3:51 PM
  • Simply use OR:

    WHERE ReferralDate BETWEEN '20080401' AND '20080430'
    OR ReferralDate BETWEEN '20090401' AND '20090430'

    --
    Plamen Ratchev
    http://www.SQLStudio.com
    • Marked as answer by akhlaq768 Tuesday, May 26, 2009 4:00 PM
    Tuesday, May 26, 2009 3:52 PM
  • how about
    where month(ReferralDate )=4
    and year(ReferralDate ) in (2008,2009,2010......)
    • Proposed as answer by wisepin Tuesday, May 26, 2009 3:54 PM
    Tuesday, May 26, 2009 3:54 PM
  • thanks
    Tuesday, May 26, 2009 4:00 PM
  • how about
    where month(ReferralDate )=4
    and year(ReferralDate ) in (2008,2009,2010......)

    That might work, but if you ran it on a table with several million records, it would be slow, because it would have to evaluate MONTH() and YEAR() and every single one of those several million records to see if the record passed the WHERE clause.

    Plamen or Michael's queries, on the other hand, will go like lightning if the table has an index on ReferralDate.
    --Brad
    Tuesday, May 26, 2009 4:00 PM