# Find() and SQL functions

**URL:** <https://www.ruby-forum.com/t/find-and-sql-functions/89029>\
**Category:** Rails\
**Created:** [March 16, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/find-and-sql-functions/89029 "2007-03-16T16:01:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre-Alexandre\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c4cdca/32.png) [@Pierre-Alexandre\_M](https://www.ruby-forum.com/u/Pierre-Alexandre_M)\
**Post date:** [March 16, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/find-and-sql-functions/89029/1 "2007-03-16T16:01:40Z")

</div>

Hi,

I have a complex SQL request to perform. I don’t want to manually do the  
whole query (find\_by\_sql) because of the complex joins, but I want to  
use MySQL functions, such as:

Model.find(:all,  
:include =\> […/…],  
:conditions =\> ["(…/…) AND  
YEAR(STR\_TO\_DATE(my\_table.my\_row,’%Y-%m-%d %H:%i:%s’)) = #{year})"],  
:select =\> […/…])

or

Model.find(:all,  
:include =\> […/…],  
:conditions =\> ["(…/…) AND  
YEAR(STR\_TO\_DATE(my\_table.my\_row,GET\_FORMAT(DATETIME,‘ISO’))) =  
#{year})"],  
:select =\> […/…])

Is there a way to combine the power of AR and SQL functions?

In the example above, I could use Ruby Time Class but what can I do if I  
want to use more specific SQL syntax (UNION, …). I guess I have to  
make the joins by hand with find\_by\_sql?

–  
,========================.  
| Pierre-Alexandre M. |  
| email : removed\_email\_address@domain.invalid |  
`========================’
