# Wildcard and mysql (find\_by\_sql)

**URL:** <https://www.ruby-forum.com/t/wildcard-and-mysql-find-by-sql/80842>\
**Category:** Rails\
**Created:** [December 31, 2006, 1:07am UTC](https://www.ruby-forum.com/t/wildcard-and-mysql-find-by-sql/80842 "2006-12-31T01:07:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jabo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jabo](https://www.ruby-forum.com/u/jabo)\
**Post date:** [December 31, 2006, 1:07am UTC](https://www.ruby-forum.com/t/wildcard-and-mysql-find-by-sql/80842/1 "2006-12-31T01:07:30Z")

</div>

Somehow I can not get the wildcard character “%” to work when using a  
MySQL find\_by\_sql statement…  
As soon as I remove the % character everything works…  
Is there a workaround, another character to be used ?

Thank you very much !

---

<div class="post-metadata">

**Author:** ![jabo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jabo](https://www.ruby-forum.com/u/jabo)\
**Post date:** [December 31, 2006, 1:36am UTC](https://www.ruby-forum.com/t/wildcard-and-mysql-find-by-sql/80842/2 "2006-12-31T01:36:59Z")

</div>

in order to use the wildcard % you need to use th LIKE statement.

select \* from table where column like ‘%’;

---

<div class="post-metadata">

**Author:** ![jabo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/aeb1de/32.png) [@jabo](https://www.ruby-forum.com/u/jabo)\
**Post date:** [December 31, 2006, 2:09am UTC](https://www.ruby-forum.com/t/wildcard-and-mysql-find-by-sql/80842/3 "2006-12-31T02:09:06Z")

</div>

Ja Bo wrote:

> Somehow I can not get the wildcard character “%” to work when using a  
> MySQL find\_by\_sql statement…  
> As soon as I remove the % character everything works…  
> Is there a workaround, another character to be used ?
> 
> Thank you very much !

Do you have to drop down to find\_by\_sql, or can you use a normal find?

Example:

pages = Page.find(:all, :conditions =\> “name like ‘Wiki%’”)

or

prefix = ‘Wiki’  
pages = Page.find(:all, :conditions =\> [‘name like ?’, prefix + ‘%’])

The key thing is that you need to put the % in the argument you are  
supplying, because when Rails does substitution into the placeholder (?)  
it will be doing escaping and quoting for you.

regards

Justin
