# Find\_by\_sql prepared statements with like

**URL:** <https://www.ruby-forum.com/t/find-by-sql-prepared-statements-with-like/123333>\
**Category:** Rails\
**Created:** [December 16, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/find-by-sql-prepared-statements-with-like/123333 "2007-12-16T18:07:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jmark](https://www.ruby-forum.com/u/jmark)\
**Post date:** [December 16, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/find-by-sql-prepared-statements-with-like/123333/1 "2007-12-16T18:07:50Z")

</div>

How does one write a prepared statement with LIKE using find\_by\_sql.

For example the problem with soemthing like this is that extra quotes  
are put around the ?  
Product.find\_by\_sql([“SELECT \* FROM products WHERE name LIKE ‘%?%’”],  
params[:name])

Thanks  
John

---

<div class="post-metadata">

**Author:** ![jmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jmark](https://www.ruby-forum.com/u/jmark)\
**Post date:** [December 16, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/find-by-sql-prepared-statements-with-like/123333/2 "2007-12-16T18:16:03Z")

</div>

On Dec 16, 2007 9:07 AM, jmark [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How does one write a prepared statement with LIKE using find\_by\_sql.
> 
> For example the problem with soemthing like this is that extra quotes  
> are put around the ?  
> Product.find\_by\_sql([“SELECT \* FROM products WHERE name LIKE ‘%?%’”],  
> params[:name])

Perhaps you could try  
Product.find\_by\_sql([“SELECT \* FROM products WHERE name LIKE ?”],  
“%#{params[:name]}%”)

Not positive it’ll work though.

Pat

---

<div class="post-metadata">

**Author:** ![jmark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e0b2c6/32.png) [@jmark](https://www.ruby-forum.com/u/jmark)\
**Post date:** [December 17, 2007, 2:23am UTC](https://www.ruby-forum.com/t/find-by-sql-prepared-statements-with-like/123333/3 "2007-12-17T02:23:35Z")

</div>

Thank you Pat. Your solution worked
