How would I go about finding all records whose first character is a number? as in find(:all, :conditions => ??????? -- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.
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]%''} ) -- Posted via http://www.ruby-forum.com/.
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060808/4547b1c4/attachment.html
> 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.