Reputation: 199
I have a database table with date field whose data type is also date and i want to fetch those recods which lie betwnn two dates.
My query is :
SELECT * FROM wp_races_entry WHERE date_in >=2012-02-08 && date_in<=2012-02-27
i also tried
SELECT * FROM wp_races_entry WHERE date_in BETWEEN 2012-02-08 AND 2012-02-27
i have records in table with date 2012-02-14
but still it return empty value.
Please help me guiding what i am missing exactly.
Upvotes: 3
Views: 2365
Reputation: 39
Try:
SELECT * FROM wp_races_entry WHERE date_in>= '2012-02-08 00:00:00' and date_in<= '2012-02-27 00:00:00'
Upvotes: 1
Reputation: 86715
2012-02-08
isn't a date, it's an integer calulation that yield the result 2002
. This is then implictly cast into a date, with 2002
meaning 2002 days from the base date
Instead, use '2012-02-08'
which is a string, which is also implicitly cast into a date, but the one you want.
SELECT * FROM wp_races_entry WHERE date_in BETWEEN '2012-02-08' AND '2012-02-27'
Upvotes: 5
Reputation: 838256
You need quotes round your dates:
SELECT * FROM wp_races_entry WHERE date_in BETWEEN '2012-02-08' AND '2012-02-27'
Without the quotes your dates are treated as arithmetic expressions: 2012-02-08 = 2002.
The query you posted is equivalent to this:
SELECT * FROM wp_races_entry WHERE date_in BETWEEN 2002 AND 1983
Upvotes: 5