Question

OK here is what I am trying to get out of my DB. I have two date fields arrivaldate and departdate and am pretty mush on how to proceed from here. I need to check for arrivaldate >='01/01/2013 and departdate <= '02/15/2014' however when I run this I obviously get the 2012's in my display results - due to the way I structured my query - What I need to do is just get out of my table on year at time or a time period ie. 01/01/2013 to 02/14/2014

using phpmyadmin for query

No correct solution

OTHER TIPS

If you're comparing two different datatypes (in your case - date columns and string literals), you shouldn't rely on the database to convert them. As you saw in your usecase, you got the dates converted to strings, and compared lexicographically. Instead, you should explicitly convert your literals to dates, and make all comparisons on that data type:

SELECT *
FROM   my_table
WHERE  arrivaldate >= STR_TO_DATE('01-01-2013', '%D-%M-%Y') 
AND    departdate  <= STR_TO_DATE('15-02-2014', '%D-%M-%Y') 

Try this:

WHERE
     STR_TO_DATE( arrivaldate, '%m/%d/%Y' ) >= '01/01/2013'
  AND
     STR_TO_DATE( departdate, '%m/%d/%Y' ) <= '02/15/2014'

Don't forget to use:

  • '%m' (lower case m) is for 2 digit month
  • '%d' (lower case d) is for 2 digit date
  • '%Y' (upper case Y) is for 4 digit year

It is always a good choice to define date specific data types on columns to store values like date, time, and date and time values.

Acceptable data types are date, datetime, and timestamp.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top