On Friday, March 25, 2016 at 10:10:21 AM UTC-7, Kaito Michishige wrote:
>
> Hi!
>
> Today I found myself writing the following:
>
>     where('EXTRACT(MONTH FROM date) = ? AND EXTRACT(YEAR FROM date) = ?', 
> d.month, d.year)
>
> and I was wondering if I could write the same with Sequel methods rather 
> than "raw" SQL, since this syntax is Postgres specific.
>
> I want to find any records that have a matching Date field, by comparing 
> only month and year.
>

For many years Sequel has supported this:

  where(Sequel.extract(:month, :date)=>d.month, Sequel.extract(:year, 
:date)=>d.year)

However, in your example you are probably better off doing:

  start = Date.new(d.year, d.month)
  finish = start >> 1
  where(:date=>start...finish)

as that is more likely to use an index.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to