On Saturday, March 21, 2020 at 12:03:00 AM UTC-7, BeeRich33 wrote:
>
> Hi there.  I have a table with dates from over the last 11 years.  I want 
> to find all "March 5th".  
>
> I'm assuming the proper way is to call the EXTRACT function:
>
> puts TW.where{Sequel.function(extract(:day, :month) == 3)}.sql       # => 
> SELECT * FROM "points" WHERE false()
>

Using the extract SQL function is a good idea.  However, there's a few of 
things wrong in this one line of code:

1)  Sequel does not override #== as that would break equality at the ruby 
level.  You can use #=~ or use a hash.

2) You don't want to call Sequel.function and pass it an SQL::Function 
object (which is what extract(:day, :month) returns in a virtual row 
block).  EXTRACT uses a special function calling sequence, it is not a 
standard function call.

3) Your closing parenthesis is not in the correct place.

So you could use on of the following:

  puts TW.where{Sequel.extract(:month, :day) =~ 3}.sql

  puts TW.where{{Sequel.extract(:month, :day) => 3}}.sql

However, if you are going to use a virtual row, you can call extract on the 
SQL::Identifier returned by day:

  puts TW.where{day.extract(:month) =~ 3}.sql

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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/4d2b062c-1315-42d9-84d6-5f3b57d908f1%40googlegroups.com.

Reply via email to