keybored
keybored

Reputation: 5248

MySQL error comparing dates

I'm trying to query my database and return with entries that are within a certain date range. As a possible simple example consider the following:

@foos = FooTracking.where('when > ?', DateTime.new(2008, 12, 22, 14, 30))

Which causes an error and dumps:

ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near 'when >= '2008-12-22 14:30:00')' at line 1:
SELECT `foo_tracking`.* FROM `foo_tracking` WHERE (when >= '2008-12-22 14:30:00')

The where call works on other columns of my table (none that have anything to do with dates), what am I missing here?

Upvotes: 2

Views: 355

Answers (1)

tgies
tgies

Reputation: 705

WHEN is a SQL reserved word. You can still use it as a column name if you always mention it in backticks like this: `when`.

Upvotes: 5

Related Questions