tags:

views:

97

answers:

4

How do I do this in MySQL?

 SELECT * FROM MyTable WHERE MyDateCol < TODAY()

I googled a lot on the subject and didn't really find anything except "look in the MySQL date ref" which does not explain the above easily enough.

I know I can do the following which is kinda indirect:

SELECT * FROM MyTable WHERE MyDateCol BETWEEN (0000-00-00) AND TODAY()
+2  A: 

In MySQL, you have the curdate() funciton, that will get you the date of today.

So, something like this should do :

select *
from your_table
where your_date_column < curdate()


Quoting the manual's page of that function :

Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.


And if you want to compare to the current time (and not only day), you can use the now() function :

Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format

Pascal MARTIN
Regarding the YYYYMMDD numeric format, would 2010-Mar-27 become 20100327 or would it be the decimal added value of 2,040
Jenko
I don't have a server on which I can try right now, but considering the manual says YYYYMMDD, I suppose you'd get 20100327, and no kind of the result of an addition -- which would be hard to work with.
Pascal MARTIN
I've verified this with my local MySQL server, it uses numbers like 20100327 which makes it really easy to do comparisons. You can also cast the string date directly to a number to use it for numeric comparison .. `"2010-03-27" => "20100327" => 20100327`
Jenko
+2  A: 
SELECT * FROM MyTable WHERE MyDateCol < CURDATE()
Quassnoi
+1  A: 

Use NOW() function:

SELECT * FROM MyTable WHERE MyDateCol < NOW()
Ivan Nevostruev
+2  A: 

If MySQL supports SYSDATE, you should use that instead of NOW() or CURDATE(), since it's more standard.

SELECT * FROM MyTable WHERE MyDateCol < SYSDATE;
Paul Tomblin