Find records that begin with a number

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

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

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?

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]%’} )

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.

Hi Richard,

if you are using mysql you might find
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs