Return to Snippet

Revision: 20254
at November 10, 2009 14:25 by mepatterson


Initial Code
def mysql_date_compare(start_date, end_date, col_name = :created_at)
  range_start = (Date.parse(start_date.to_s) - 1.day).strftime("%Y-%m-%d 23:59:59")
  range_end = (Date.parse(end_date.to_s) + 1.day).strftime("%Y-%m-%d 00:00:00")
  "#{col_name.to_s} BETWEEN '#{range_start}' AND '#{range_end}'"
end

Initial URL

                                

Initial Description
requires Rails libraries.  could be rewritten to just use core Ruby libs, but I'm too lazy right now and I use this entirely in my Rails apps.

Initial Title
generate a mysql BETWEEN clause to find within a date range

Initial Tags
mysql, date, rails, ruby

Initial Language
Rails