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
Related
Joomla, mysql database. laqdf_base Table qdf_joomleague_person: in knvbnr field you can see numbers in different formats 22.333.333 and 12345678.
What exactly (for a very fresh newbie) do I have to do, within PhpMyAdmin to update the table so all numbers in knvbnr shows numbers in 12345678 format (no "." in them)
I cant post images cause I dont have 10 points yet! Sorry
You can use MySQL's replace() function. Example:
update qdf_joomleague_person set knvbnr = replace(knvbnr, '.', '');
I have created a database table in mySQL of which two column names are "landPhone" and "mobilePhone" to store phone numbers (in the format of: 123-456-8000 for land and 098-765-6601 for mobile). These two columns' data type are set to VARCHAR(30). The data have been inserted in the table. But after SQL query, I found the phone numbers have been truncated. It shows (above two data for example) only first 3 digits (123) for landPhone and only first 2 digits after removing the leading '0' (98) for mobilePhone.
Why this is happening ?
Phone numbers are not actually numbers; they are strings that happen to contain digits (and, in your case, dashes). If you try to interpret one as a number, two things typically happen:
Leading zeros are forgotten.
Everything from the first non-digit to the end of the string is stripped off.
That sounds exactly like the result you're describing. Even if you end up stuffing the result into a string field, it's too late -- the data has already been corrupted.
Make sure you're not treating phone numbers as integers at any point in the process.
You must use
insert into sample values('123-456-8000', '098-765-6601' )
instead of
insert into sample values(123-456-8000, 098-765-6601 )
see this SQLFiddle.
Thanks all for your solution. As cHao suspected, it was me who did the mistake. When I first time created the table, I declared the datatype of the phone columns as INT, later I corrected them to VARCHAR().
When I dropped the table and inserted the same data to the new table, it is working fine.
That sounds exactly like the result you're describing. Even if you end up stuffing the result into a string field, it's too late -- the data has already been corrupted. ..cHao
Question to understand: Why mySQL doesn't override the previous datatype with the new one ?
In a MySQL table, I want to store the numeric value unicode form of Gujarati file name RS and also I want to get data using group by query using sum function then how I can?
For example I have table:
a(no int,RS int/varchar(10)utf8_unicode_ci)
and I get data using this query:
select sum(RS) from a
insert data:
INSERT INTO `a` (`no`, `rs`) VALUES
(10, '૧૨૩'),
(9, '૧૦૦'),
(8, '૧૨');
If you want to perform arithmetic manipulations on a column the only practical approach is to save the values in a true numeric column. Trying to save numbers as text and constantly converting them back and forth to numbers would not only be a nuisance, it could very well introduce any number of subtle little bugs into your application.
In other words, you are confusing the data with the presentation of the data. If you store the numbers as numbers at the data level then the presentation logic can format those numbers in any number of different ways. Probably the lowest-level place to look for ways to present numbers as Gujarati numbers would be the "Use native digits" setting in Windows' "Regional and Language Options" control panel:
I'm using RODBC to interface R with a MySQL database and have encountered a problem. I need to join two tables based on unique ID numbers (IDNUM below). The issue is that the ID numbers are 20 digit integers and R wants to round them. OK, no problem, I'll just pull these IDs as character strings instead of numeric using CAST(blah AS CHAR).
But R sees the incoming character strings as numbers and thinks "hey, I know these are character strings... but these character strings are just numbers, so I'm pretty sure this guy wants me to store this as numeric, let me fix that for him" then converts them back into numeric and rounds them. I need to force R to take the input as given and can't figure out how to make this happen.
Here's the code I'm using (Interval is a vector that contains a beginning and an ending timestamp, so this code is meant to only pull data from a chosen timeperiod):
test = sqlQuery(channel, paste("SELECT CAST(table1.IDNUM AS CHAR),PartyA,PartyB FROM
table1, table2 WHERE table1.IDNUM=table2.IDNUM AND table1.Timestamp>=",Interval[1],"
AND table2.Timestamp<",Interval[2],sep=""))
You will most likely want to read the documentation for the function you are using at ?sqlQuery, which includes notes about the following two relevant arguments:
as.is which (if any) columns returned as character should be
converted to another type? Allowed values are as for read.table. See
‘Details’.
and
stringsAsFactors logical: should columns returned as character and
not excluded by as.is and not converted to anything else be converted
to factors?
In all likelihood you want to specify the columns in questions in as.is.
I want to select only the area code from a list of column entries populated by phone numbers. This is what I have:
SELECT LEFT(phone, 3) AS areacode, COUNT(phone) AS count
FROM registration
GROUP BY areacode;
The problem is, the entries aren't consistent. So some phone numbers start as +123-456-7899, and others with (123)-456-7899, and others with no symbol at the beginning.
So my question is: is there a way that I can ensure the SELECT LEFT starts at the first integer?
Thanks!
There are somethings that SQL is just not meant for. This is one. I would select the phone number into a string, and do some pattern matching in your programming language of choice to find the area code.
-OR-
Change your table such that area code is a different column.
Two options (neither of which being SQL):
Select all phone numbers and use a programming language of your choice to programatically strip out the unnecessary characters.
Clean the input to strip out all unnecessary characters prior to inserting them into the database
SQL is not the best way to do this, rather, SQL + programming
There actually is a way to do this in SQL that was intentionally designed for this exact purpose.
SELECT SUBSTRING(office_phone_number, 1, 3) FROM contact;
Of course, this depends on how the number is stored in the table. If parenthesis are present, your starting position would be off.
Here is more information:
MySQL substring function