SQL Query not getting executed - returns empty set

I have a database table with a date field whose datatype is also date, and I want to get these recommendations that lie between two dates.

My request:

SELECT * FROM wp_races_entry  WHERE date_in >=2012-02-08 && date_in<=2012-02-27

      

I have also tried

SELECT * FROM wp_races_entry WHERE date_in  BETWEEN 2012-02-08 AND 2012-02-27

      

I have records in a table with a date 2012-02-14

, but when doing so, it returns blank.

Please help me identify what I am not seeing exactly.

+3


source to share


3 answers


You need quotes around 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 request you posted is equivalent to this:

SELECT * FROM wp_races_entry WHERE date_in BETWEEN 2002 AND 1983

      

+5


source


2012-02-08

is not a date, it is an integer value that gives the result 2002

. This is then forced into the date, whereby 2002

means2002 days from the base date

Instead, use '2012-02-08'

which is a string that is also implicitly cast to the date, but which one you want.



SELECT * FROM wp_races_entry WHERE date_in BETWEEN '2012-02-08' AND '2012-02-27'

      

+5


source


Try:

SELECT * FROM wp_races_entry WHERE date_in>= '2012-02-08 00:00:00' and date_in<= '2012-02-27 00:00:00'

      

+1


source







All Articles