How to replace all special characters and spaces in a table column? - mysql

I have a table named "contacts" with below fields
id | phone_mobile
1 | 9988011223
2 | 00-12-123456
3 | 91-8876980987
4 | (91) 88990099777
I want a select query which will return below output
id | phone_mobile
1 | 9988011223
2 | 0012123456
3 | 918876980987
4 | 9188990099777

For a known set of characters you can use replace() function chaining something as
mysql> select replace(replace(replace('00-12-123456','-',''),'(',''),')','');
+----------------------------------------------------------------+
| replace(replace(replace('00-12-123456','-',''),'(',''),')','') |
+----------------------------------------------------------------+
| 0012123456 |
+----------------------------------------------------------------+
So in your case it could be
select
id,
replace(replace(replace(replace(phone_mobile,'-',''),'(',''),')',''),' ','') as phone_mobile
from table_name
If there is a long list of characters to be replaced then its better to use the application level to do the job since the chaining of replace becomes really messy.

Related

MySql: Select Distinct for words in different order

I have problem with creating query, which getting no duplicate values form my table. Unfortunately, Full Name column has Name and Surname in different order.
For example:
+----+----------------------+
| ID | Full Name |
+----+----------------------+
| 1 | Marshall Wilson |
| 2 | Wilson Marshall |
| 3 | Lori Hill |
| 4 | Hill Lori |
| 5 | Casey Dean Davidson |
| 6 | Davidson Casey Dean |
+----+----------------------+
I would like to get that result:
+----+-----------------------+
| ID | Full Name |
+----+-----------------------+
| 1 | Marshall Wilson |
| 3 | Lori Hill |
| 5 | Casey Dean Davidson |
+----+-----------------------+
My target is to create query, which getting in similar way, for example: select distinct for Name and Surname in the same order.
Any thoughts?
It requires lots of String operations, and usage of multiple Derived Tables. It may not be efficient.
We first tokenize the FullName into multiple words it is made out of. For that we use a number generator table gen. In this case, I have assumed that maximum number of substrings is 3. You can easily extend it further by adding more Selects, like, SELECT 4 UNION ALL .. and so on.
We use Substring_Index() with Replace() function to get a substring out, using a single space character (' ') as Delimiter. Trim() is used to remove any leading/trailing spaces left.
Now, the trick is to use this result-set as a Derived table, and do a Group_Concat() on the words such that they are sorted in a ascending order. This way even the duplicate names (but substrings in different order), will get similar words_sorted value. Eventually, we simply need to Group By on words_sorted to weed out the duplicates.
Query #1
SELECT
MIN(dt2.ID) AS ID,
MIN(dt2.FullName) AS FullName
FROM
(
SELECT
dt1.ID,
dt1.FullName,
GROUP_CONCAT(IF(word = '', NULL, word) ORDER BY word ASC) words_sorted
FROM
(
SELECT e.ID,
e.FullName,
TRIM(REPLACE(
SUBSTRING_INDEX(e.FullName, ' ', gen.idx),
SUBSTRING_INDEX(e.FullName, ' ', gen.idx-1),
'')) AS word
FROM employees AS e
CROSS JOIN (SELECT 1 AS idx UNION ALL
SELECT 2 UNION ALL
SELECT 3) AS gen -- You can add more numbers if more than 3 substrings
) AS dt1
GROUP BY dt1.ID, dt1.FullName
) AS dt2
GROUP BY dt2.words_sorted
ORDER BY ID;
| ID | FullName |
| --- | ------------------- |
| 1 | Marshall Wilson |
| 3 | Hill Lori |
| 5 | Casey Dean Davidson |
View on DB Fiddle

MySQL - add text prefix before column name in SELECT statement

Here is my table:
| ID | NUMBER |
| 1 | 523 |
| 2 | 293 |
| 3 | 948 |
And now, I want to get all NUMBER values but I want to add in result two numbers - 48 - (without upadting existing results). So finally I want print these results:
| NUMBER |
| 48523 |
| 48293 |
| 48948 |
So I need a query, something like this:
SELECT '48' + `number` FROM `table`
but this query doesn't work fine (this query only update column name from NUMBER to 48 + NUMBER).
Any ideas?
Thanks.
You need CONCAT
SELECT CONCAT('48' , `number`) AS number FROM table
Demo

Count all same comma separated value in one column mysql

I have 2 table and there's a column which contains comma separated value and I want to count all same value of that column, here's the sample table:
Client table
ID | Name | Procedure
1 | Joe | Samp1,Samp2
2 | Doe | Samp1,Samp2,Samp3
3 | Noe | Samp1,Samp2
Desire Output
Summary table ( For Procedure )
ID | NAME | COUNT
1 | Samp1 | 3
2 | Samp2 | 3
3 | Samp3 | 1
Now, do you have any idea or suggestion so i can make it happen ? like add new table or is this possible with single query ?
Try this query
SELECT LENGTH(COLUMN) - LENGTH(REPLACE(COLUMN, ',', '')) FROM TABLE_NAME

Best way to find a line in MySQL by piece of value

Database:
+----+------------+
| id | somevalue |
+----+------------+
| 1 | 1000 |
+----+------------+
| 2 | 1001 |
+----+------------+
| 3 | 1002 |
+----+------------+
| 4 | 10021 |
+----+------------+
Question:
What is the best way to find a row by a string which contains piece of "somevalue"?
1. Let's say string is 1002123456. So in this case I must find row with ID 4.
2. Let's say string is 1002345678. So in this case I must find row with ID 3.
Would "MySQL LIKE" work in this scenario?
UPDATE:
Database has 60k rows and "somevalue" should be matched from the front. Because piece of "somevalue" might contain different rows, for example:
+----+------------+
| id | somevalue |
+----+------------+
| 1 | 1000 |
+----+------------+
| 27 | 371000 |
+----+------------+
I am looking for a way to make the process as fast as possible.
SELECT * FROM `db`.`table` WHERE 'someval' LIKE CONCAT('%',`field` , '%') ;
The RLIKE allowes you to match regular expressions in the fields
SELECT * FROM 'table' WHERE 'searchNumber' LIKE CONCAT('%', somevalue, '%')

Calculate the words number in a view in mysql

I have the following table structure
+-----------------+-------------+
| myID | text |
+-----------------+-------------+
| 3 | some text |
| 3 | other text |
| 5 | text |
+-----------------+-------------+
myID is not a unique ID. It can be repeated. Text is arbitrary strings.
I'm looking to create a view that returns the count of words in the text of the myID rows.
+-----------------+-------------+
| myID | count |
+-----------------+-------------+
| 3 | 4 |
| 5 | 1 |
+-----------------+-------------+
Well, I started out (I'm a newbie is mySQL) and did the following
SELECT
myid,
pagetext
FROM table
GROUP by myid
This does just the grouping. I think the next steps are
How to make pagetext returns the concatenation of the appropriate myID rows (I'm looking to limit the concatenation to 25 rows only).
How to count the number of words in the returned value.
Any ideas how to start?
You can find the number of words in a column (if you can assume there is 1 and exactly 1 space between words) with the following query:
SELECT
myId,
SUM(LENGTH(text)-LENGTH(REPLACE(text, ' ', ''))+1)
FROM table
GROUP BY myId
Source: http://www.mwasif.com/2008/12/count-number-of-words-in-a-mysql-column/