ActiveRecord search on field with criteria - mysql

I'm relatively new to Rails and ActiveRecord. I'm storing phone numbers in ActiveRecord fields (phone_number) that can be in a number of formats "+1 928 929 3829" or "393 938 0293" or "392-937-9283" (you get the idea).
Later I'm capturing a 10 digit phone number (so it will always be like "3939380293") and then trying to match that to my ActiveRecord phone number field. The operation I want to do is match against the last 10 numeric characters of phone_number.
I've tried looking at the different SQL-type query constraints that you can use in queries, but I'm having trouble finding exactly what I need or making the logic leap.
Can anyone offer some advice here?

I would do followings
1 - having a VARCHAR field for phone number, which will save numbers in any format (Ex: "+1 928 929 3829" or "393 938 0293" or "392-937-9283")
2 - Have a Activerecord to select the phone number with a regex.
My most favourite regex writers are rubular, regexper

Related

Mysql compare comma-separated field with single string

So a field called schools in the database might have a value of:
'13,121,112,1212'
I'm using that to show the potential for a mistake.
Suppose I'm looking for a value of 12 in that field. The commas denote a "whole number" and I don't want to match 112 or 1212
Is there a more elegant match than this?
#compare = 12;
WHERE CONCAT(schools,',') LIKE CONCAT('%',compare,',%)
I was recently impressed by the GROUP_CONCAT function but this is kind of in reverse of that. Thanks!
For this simple case you can use FIND_IN_SET();
WHERE FIND_IN_SET('13', schools);
Note though that there is no good indexing for columns with comma separated text, so the queries will be much slower than a normalized database.

Mysql Like characters [a-z] or Zero

In a mysql table i have column whit this info..
Col.
tr10
tr210
zbr10
00010
10010
tr 10
The question is simple, i need to find in a mysql query all the records number 10.. but as you can see in the example not 10010 etc..
Result:
tr10
zbr10
00010
tr 10
I know is a mess but the records had to be load in that form..
so you have characters at the begining, in some cases spaces, or zeros..
An option could be extract (by hand) hundred of characters to another column to keep the things less complex, but at the same time i still having problems with the 000010 values..
Use regular expressions
select * from table where col regexp '^[a-z]+10$'
Play with the regex until you get your desired results, i didnt fully understand you criteria so I just made one up but the one in my example will pull all the rows with any alpha characters proceeded by 10

Stripping Phone Numbers of Non-numeric Values During SQL Query in Rails

I have a list of new restaurants and I want to check them against a database of restaurants using the phone number. The problem with phone numbers is that they have different formatting in the database (i.e. 123-345-6789 or (123)-456 8988).
So far, I have created an index for the phone so that I search through them quickly. I also stripped the phone number of the new restaurants so that they only contain numeric values (0-9).
Now, I want to strip the databases' numbers right before I compare so that I will strictly comparing numbers.
#strip the phone of non-numeric values and spaces
formatted_phone = SunspotHelper.sanitize_term(pr.phone).gsub(/\s+/, "")
Restaurant.where(contact_phone: formatted_phone).each do |r|
#irrelevant code here
end
The problem with the above code is that contact_phone (from database) will be in its non-stripped format. My research so far has lead me to believe that I have to use something like REGEXP_LIKE, but I'm not quite sure how to implement this with Rails. I do NOT want to update the database - I simply want to strip the numbers for comparison while still keeping the original formatting in the DB.
How do I strip the phone numbers, contact_phone, of non-numeric values from the database before I compare it with formatted_phone?
If you'd just like to limit your effort to the Rails query (instead of properly formatting the phone numbers in another field in the database) here's how you could use your DB engine's REGEXP capabilities
Restaurant.where(["REGEXP_REPLACE(contact_phone, '[^[:digit:]]', '') = ?", formatted_phone]).each do |r|
#irrelevant code here
end

How to enforce custom MySQL column format

What if I wanted to define a custom column format in MySQL? The custom format that I want is always a 2 digit integer, followed by the # sign, then a space, then a decimal number with 2 digits after the decimal. Examples of allowed values are like this:
30# 11.00
27# 17.25
40# 17.25
values that are not allowed are like this:
30# 11
40# 20.5
some string
Is this possible in MySQL? If so, any hints on how to do it?
well since "The CHECK clause is parsed but ignored by all storage engines." in mysql, your only remaining option would be to add a before insert/update trigger on your table to regexp your input and proceed.

MySQL Regex - Find where there are 10 numbers in a row in a field

I need to find records where there are 10 numbers in a row in the field. e.g. 1234567890, 8884265555 etc. The field will contain text as well so I need to see if any 10-digit strings exist anywhere within the field.
I have got this far...
SELECT * FROM `comments` WHERE detail REGEXP '[0-9]{10}'
My that returns where there 10 numbers anywhere in the field instead of all in a row. I am trying to detect phone numbers. Thanks!
The regular expression [0-9]{10} does imply that ten digits in a row (only) should be matched. So, your issue must be elsewhere.