# Find records that begin with a number

**URL:** <https://www.ruby-forum.com/t/find-records-that-begin-with-a-number/101933>\
**Category:** Rails\
**Created:** [August 8, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/find-records-that-begin-with-a-number/101933 "2006-08-08T18:53:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [August 8, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/find-records-that-begin-with-a-number/101933/1 "2006-08-08T18:53:25Z")

</div>

How would I go about finding all records whose first character is a  
number?

as in find(:all, :conditions =\> ???

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [August 8, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/find-records-that-begin-with-a-number/101933/2 "2006-08-08T19:41:53Z")

</div>

Richard wrote:

> How would I go about finding all records whose first character is a  
> number?
> 
> as in find(:all, :conditions =\> ???

or can you only do this with find\_by\_sql ?

Anyone?

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [August 8, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/find-records-that-begin-with-a-number/101933/3 "2006-08-08T20:12:29Z")

</div>

Richard wrote:

> How would I go about finding all records whose first character is a  
> number?
> 
> as in find(:all, :conditions =\> ???

Depends on your database, but if it supports the SQL99 SIMILAR TO  
operator you should be able to do something along the lines of this:

find( :all, :conditions =\> %q{field SIMILAR TO ‘[0-9]%’} )

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [August 15, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/find-records-that-begin-with-a-number/101933/4 "2006-08-15T14:12:32Z")

</div>

> Richard wrote:
> 
> > How would I go about finding all records whose first character is a  
> > number?
> > 
> > as in find(:all, :conditions =\> ???
> 
> or can you only do this with find\_by\_sql ?

What “first character” ? Let’s assume you have a table of ‘peoples’  
that  
have a “first\_name” field… and you wanted all the names that begin  
with  
‘P’, you could do:

People.find(:all, :conditions =\> “first\_name LIKE ‘[pP]%’”)

Might not be super efficient depending on your database., but it would  
work.

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [August 8, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/find-records-that-begin-with-a-number/101933/5 "2006-08-08T23:09:41Z")

</div>

Hi Richard,

if you are using mysql you might find  
[http://dev.mysql.com/doc/refman/5.0/en/regexp.html](http://dev.mysql.com/doc/refman/5.0/en/regexp.html) an interesting read.  
Since the find method with submitted conditions will fit them in the  
where  
clause you’ll have to use find\_by\_sql imho.

Don’t know about the perfomance.

Cheers,  
Jan
