trueCamelType trueCamelType - 1 year ago 71
Ruby Question

How can I select distinct dates from a database, when it's formatted as DateTime

I have a database setup similarly to this

The output of

sqlite> PRAGMA table_info(mytable);



And it looks like this

| 0 | 2016-10-11 12:10:22|
| 1 | 2016-10-11 12:11:22|
| 2 | 2016-10-12 10:45:45|
| 3 | 2016-10-12 11:12:12|

In Ruby on Rails, I'd like to select all of the rows with the same date (ignoring time). And I'm looping through them, to do something for every date. For example:

If I had the same database, and instead of
it was formatted as just the
I would do something similar to below:

distinctDate =

distinctDate.each do |x|
put x

But how can I write the select with the distinct, and it also ignore the time?

Joe Joe
Answer Source
MyTable.pluck("distinct date(updated_at)") 

That will give you back an array of distinct dates, then you can process them however you need to in the application.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download