I have one phone field column which contains phone numbers like that
'123456789'
'123-456-789'
etc
means it contain 9 digit number or number + hyphen.
I want to make a SQL query which updates all records in 'xxx-xxx-xxx' format.
I have made few attempts but cannot get exact solution.
Please any one help me.
Thanks in advance.....
use something like
UPDATE mytable SET phone =
CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 3))
Also to only get the rows that are missing hyphens you would say WHERE phone not like '%-%'
Close. You'd first need to test if the string contained a '-' before adding more, but that's the right track.
UPDATE mytable
SET phone = CONCAT(SUBSTRING(phone, 1, 3),'-',SUBSTRING(phone, 4, 3),'-',SUBSTRING(phone, 7, 4))
where INSTR( phone, '-' ) = 0;
Related
In MYSQL, I want to check whether mobile no. exist or not. But here I have totally different scenerio.
Like in my table I have MobNo. = '9874563210'. I want to check if '9874563210' or '+189874563210' or '09874563210' or '009874563210' exist or not.
NOTE: '+18' is country code. And I want to check for 1 number only i.e. ('9874563210') others '+18', '0', '00' before MOBILE-NUMBERS are scenerios
I tried is: SELECT MobNo. FROM tbl_Contact WHERE MobNo. LIKE '%9874563210%'. This query only check for '9874563210' only.
I want it should check all condition say: '9874563210', '+189874563210', '09874563210', '009874563210'
I want if mobile number '9874563210' including scenerios '+18', '00', '0' as prefix exist or not. If exist then say "ALREADY EXIST"
I tried many solution but none work for me. I am very new to MYSQL.
ANY HELP, WILL BE HIGHLY APPRECIATED.
This is not as much a question on SQL, as it is on phone numbers and algorithm :-)
Telephone numbers are complete when starting with a plus sign. +1234567890 might be such a number, where after the plus there comes some country code, then some area code, then the local number.
If on the other hand a number doesn't start with a plus sign, the number is somewhat incomplete, because it can't be used from everywhere in the world. Instead it is a number based on some location.
I live in Germany, Hamburg. Country code is 49, area code is 40. Moreover we use a prefix 0 to call an area and 00 to call a country. Hence these are the same when stored on a phone in Hamburg:
+4940123456
004940123456
040123456
123456
A number in another area in Germany can be stored on my phone as
+4930123456
004930123456
030123456
A number in another country can be stored on my phone as
+5512345678
005512345678
I can convert any of all the above numbers to the international complete number, because I know the country prefix, the area prefix, my country code and my area code. The algorithm is as follows:
IF the number starts with '+', it's complete already.
ELSEIF the number starts with the country prefix ('00' for me), then I replace the country prefix with a '+'.
ELSEIF the number starts with the area prefix ('0' for me), then I replace the area prefix with a '+' followed by my country code ('49').
ELSE I put a '+', then the country code ('49'), then the area code ('40') before the number.
I'll end up with a query like this:
select *
from address
where
case
when phone like '+%' then phone
when phone like '00%' then concat('+', substr(phone, 3))
when phone like '0%' then concat('+49', substr(phone, 2))
else concat('+4940', phone)
end =
case
when #phone like '+%' then #phone
when #phone like '00%' then concat('+', substr(#phone, 3))
when #phone like '0%' then concat('+49', substr(#phone, 2))
else concat('+4940', #phone)
end;
If phone numbers can contain additional characters (like dashes and slashes, e.g. '+49-40/1234-56), then you would have to invoke REPLACE on the numbers, too, in order to get rid of these characters and be able to compare.
use in
SELECT MobNo FROM tbl_Contact
WHERE MobNo in ('9874563210', '+189874563210', '09874563210', '009874563210')
you will get only the exist number other's will not appear
Join your table to a query that returns all possible scenerios:
select t.mobno
from tbl_Contact t inner join (
select '' prefix union all
select '+18' union all
select '00' union all
select '0'
) p on concat(p.prefix, '9874563210') = t.mobno
You can extend the query of scenarios with as many values as you want.
In SQL, 'IN' statements creates a series of OR statements. In your case, if you need to check multiple phone numbers, you will need combination of IN and LIKE
If the data type of PhoneNumber is string, the below should work.
select * from PhoneNumber where PhoneNumber like '%1234567890' OR PhoneNumber like '%1111111110'
Use regular expressions:
where mobno regexp '^(+18|0{0-2})9874563210$'
I have a column with credit card numbers in different formats. Some have dashes some don't. I need the first character and the last four formatted like this:
5-1234
Just want to run a UPDATE query from the console. I know how to get the first and last but can't get the union right. Here is my code.
UPDATE table
SET credit_no = LEFT(credit_no, 1)
union "-"
union SUBSTRING(credit_no, -4, 4);
Try something like this:
UPDATE table SET credit_no = CONCAT(LEFT(credit_no, 1), "-", RIGHT(credit_no, 4));
Or even:
UPDATE table SET credit_no = CONCAT_WS('-', LEFT(credit_no, 1), RIGHT(credit_no, 4));
See Docs: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html
My table contains some columns with ;-separated numbers like this :
1;2;43;22;20;12
and so on. It's also possible there's only 1 number in this column like 110, or 2 numbers like this 110;143
I want to select the rows that contain a certain number in this column. The number is in a variable $search_var.
Let's say I need to search for the number 1 in my column. If I use a select with like statement like so :
"SELECT * FROM Table WHERE ids LIKE '%".$search_var."%'"
I get all results containing '1' and not only '1', so I also get 11, 14, 110, 1999 etc.
I think I need to use some sort of regex-statement but I'm lost here... Who can help ?
You might not need regex for this
Set #YourNUmber := 110;
SELECT *
FROM Table
WHERE ';' + ids + ';' LIKE '%;'+ #yourNumber + ';%'
This guarantees there are always ; surrounding all the numbers.
This is formatted for SQL Server. The variable syntax and wildcards might be different if you are using something else.
EDIT:
Thanks #FĂ©lixGagnon-Grenier for the suggestions. I think either of these two will work. See here for a SQL Fiddle example
SELECT *
FROM T
WHERE concat(';',ids,';') LIKE concat('%;', #YourNumber , ';%');
SELECT *
FROM T
WHERE LOCATE(concat(';', #YourNumber , ';'),concat(';',ids,';'))>0
Try this solution if you're using SQL Server. This searches for the number where adjcent characters are not numbers:
SELECT * FROM Table WHERE ids LIKE '%[^0-9]".$search_var."[^0-9]%'
I have a table with 8 columns, all varchar (24, 24, 24, 5, 5, 255, 255, 255).
The three important columns are:
icd10code (varchar 24)
icd10codedot (varchar 24)
icd10length (varchar 5)
I wish to convert one column that looks like this - "ABCDEF" to this - "ABC.DEF".
Here is the MySQL query used, and it worked perfectly!
UPDATE icd10
SET icd10codedot =
CONCAT ( LEFT(icd10code, 3) ,
"." ,
RIGHT( icd10code, ( LENGTH(icd10code)-3 )
)
);
All 91,000 rows came out nice EXCEPT there was a trailing "." at the end of those entries that only had 3 characters - like this: "ABC.".
I don't want that dot at the end.
So I tried this:
UPDATE icd10 SET icd10codedot = LEFT(icd10codedot, 3) WHERE LENGTH(icd10codedot)=4;
and this:
UPDATE icd10 SET icd10codedot = LEFT(icd10code, 3) WHERE LENGTH(icd10code)=3;
and this:
UPDATE icd10 SET icd10codedot = LEFT(icd10code, 3) WHERE LENGTH(icd10code)=3;
and none of them worked (a bunch of other combinations were also run, but none worked).
So to see how things were doing in the code, I made a column called "icd10length" varchar int and did this query:
UPDATE icd10 SET icd10length = LENGTH(icd10code);
and all I got was 91,000 "7"s lined up on the right of the column.
I changed the column from INT to VARCHAR 5, and reran the query and got 91000 "7"'s lined up on the RIGHT of the column ( :-) ).
So two questions:
How can I get the correct length of the variable icd10code or icd10codedot into a column?
Do you have a slick way of clipping the last character of a string that is 4 characters long?
Thanks very much again!
PS - I tried the initial query in PHP and it crashed - timing out after 300 seconds. Found on the web that you should always do things in MySQL whenever possible.
PSS - I'm doing all of this via phpMyAdmin.
It seems you have empty space at the end, try using rtrim.
Here is an example table on which I would like to execute a query:
Structure of table_1
number | photos (CHAR,4)
1234 | 1210
I would like to update value from column photos, but without changing the whole value. I would like change, for example, only third character to "2" without knowing the whole value. How can I do that?
I know I could do that in this way described below, but the problem is the value may be variable and it is a column type CHAR, not INT.
UPDATE table_1 SET photos = (photos + 10) where number='1234'
Yes with mid, left and right functions because photos is a type char:
UPDATE table_1 SET photos =
concat(
left( photos, 2),
'1',
right( photos, 1)
)
where number='1234'
Use concat(), left() and substring()
The example above would look like this:
update table_1
set photos = concat(left(photos, 2), "2", substring(photos, 4))
where number = '1234'
The advantage to this over left/right, is this will work for variable lengths of "photos".
Looking at it more generally, if you want to set the xth position to "2":
update table_1
set photos = concat(left(photos, x-1), "2", substring(photos, x+1))
where number = '1234'
(NOTE: I don't have MySQL running right now so I can't test the above. There are certain to be off-by-one errors which should be easy for your to correct)
If the column contains only numbers then the example you gave would work. The value will be converted to an integer, added to, and then converted back.
You might want to explain your reasoning for wanting to do this though. It seems a bit strange.