2

Is there any way to query a table with ActiveRecord with a Day Of week condition ? In the way that it can be compatible with all RDBMS ? E.g. :

Event.where("DAYOFWEEK(created_at) = 2")

This example would work with MySQL but wouldn't with Postgres, Or SQLite

Alexis Darnat
  • 581
  • 6
  • 13
  • I'm pretty sure you can't make it work for all databases, but Postgres lets you `SELECT EXTRACT dow FROM [timestamp]`. https://www.postgresql.org/docs/8.1/functions-datetime.html – Austin Mullins Dec 05 '18 at 22:58
  • Actually, it looks like MySQL has the same EXTRACT syntax: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_extract – Austin Mullins Dec 05 '18 at 23:00
  • Looks like SQLite makes you use strftime instead: https://www.sqlite.org/lang_datefunc.html – Austin Mullins Dec 05 '18 at 23:03
  • Thank you very much @AustinMullins , I already looked at those documentations. My issue is that I would like to make it compatible with all those RDBMS. Maybe, there is a way of extending ActiveRecord to provide the implementation of this function ( DAYOFWEEK() ) for each Adaptor ? – Alexis Darnat Dec 05 '18 at 23:10

0 Answers0