Arel Query CAST operation
Asked Answered
T

1

8

I need to form a query in Arel which had a CAST operation earlier. The original query was like : select * from tablename where tablename.anniversary >= CAST(STR_TO_DATE(?,'%d-%m-%Y-%k-%i-%s') as DATETIME)

(the question mark is replaced by actual date in further code)

For the where condition, I am doing this :

where(tablename['anniversary']
                       .gteq("CAST(STR_TO_DATE(#{date_value},'%d-%m-%Y %k:%i') as DATETIME)"))

The final resulting query that I should get should have : tablename.anniversary >= '2015-07-13 16:12:00'

But I get :

tablename.anniversary >= 'CAST(STR_TO_DATE(13-09-2015 05:33PM,\'%d-%m-%Y %k:%i\') as DATETIME)'

What am I doing wrong ?

Tend answered 14/9, 2015 at 12:16 Comment(0)
M
5

I'm not sure that understand your question, but...

First why don't you just use Ruby datetime formating ?
But if this date is a column:

Arel::Nodes::NamedFunction.new('CAST', [Model.arel_table[:column_name].as(Arel::Nodes::Quoted.new('DATETIME'))])
Multiplex answered 23/1, 2016 at 22:56 Comment(2)
Arel::Nodes::Quoted('DATETIME') doesn't seem to work, says it's undefinedCerebrovascular
I edit it, thanks. It's Arel::Nodes::Quoted.new('DATETIME'), but if it works depends on your DB.Multiplex

© 2022 - 2024 — McMap. All rights reserved.