Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arel Query CAST operation

Tags:

sql

arel

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 ?

like image 835
iAmInTrouble Avatar asked Sep 14 '15 12:09

iAmInTrouble


1 Answers

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'))])
like image 64
Kamen Kanev Avatar answered Sep 24 '22 10:09

Kamen Kanev