Subscribe to this feed

Navigation

Recent Posts

Archive

Using datetime stamp in MySQL query

Tuesday 16 Jan, 2007 - 18:59pm | 0 comments |

I would have to describe this as a hack rather than anything else as the convert function wouldn't work for me and date() is not supported, i.e. you can't use "SELECT date(timestamp_field) from db"; to extract only the date element; whereas you can use year(timestamp_field) or month(timestamp_field).

I needed to filter records based on the date, to extract rows from the table where the date was equal to today. The date field in the table was a datetime column. My workaround was to use the left function and extract the ten characters of the date which I could then compare to today as in the following.


$query = "SELECT title, contents FROM articles WHERE left(timestamp_field,10) = '$date'";

This worked. I don't think it's the elegant solution, alternatives welcomed.

Posted in: Business
Tags: SQL | MySQL | timestamp | date function

Comment
 | Link | back to top | del.icio.us digg it furl reddit

© Eriginal Ltd 2011, all rights reserved