Reputation: 4995
I have a database like this:
TABLE Name | timestamp ------------------------------------------ Hello | 2013-02-23 14:26:18 Yo | 2013-03-22 14:26:18
I want to do a SQL Select where only things that are in the past (e.g. the timestamp isn't set for the future) are selected.
What would be the best way to do that?
Thanks for any help.
EDIT:
I tried the suggestions but none of them seem to work, here is the query:
SELECT * FROM TABLE WHERE timestamp<NOW()
And it still selected everything, I also tried:
SELECT * FROM TABLE WHERE timestamp<CURRENT_TIMESTAMP
And it still selected everything again.
It's really wierd, when I use "timestamp>CURRENT_TIMESTAMP" it seems to work (it hides all items not from the future) but not when I try the other direction which makes no sense. Could it be a timezone issue or something like that?
Upvotes: 6
Views: 3323
Reputation: 16314
Have you tried
SELECT *
FROM yourtable T
WHERE T.timestamp < NOW();
...?
NOW()
...
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
Upvotes: 1
Reputation: 53246
SELECT `Name` FROM `TABLE` WHERE `timestamp` < NOW();
Should get you what you want.
Upvotes: 5
Reputation: 7239
Try the following query :
SELECT * FROM YourTable WHERE 'timestamp' < CURRENT_TIMESTAMP
Upvotes: 1