How to remove multiple strings in a Mysql column - mysql

I would like to trim this string (these strings are stored in a single column)
[17332] Rohayati Bakri [20611] Nazrie Haslieanie [20612] Nur Izzati
into this (also stored in a single column)
Rohayati Bakri, Nazrie Haslieanie, Nur Izzati
using only Mysql query. The reference id -> [xxxxx] all have the same length.
Is it possible?

Unfortunately, MySQL doesn't support any kinds of regular expressions in REPLACE. A brute force method is:
Dump the table
Replace strings with sed
Import table
The commands are:
mysqldump -u login -p db your_table > dump.sql
sed 's/\[[0-9]\{5\}\]//g' dump.sql > newdump.sql
mysqlimport -u login -p db your_table < newdump.sql
Potentially this could affect some other data that matches the pattern, so use this on your own risk.

You can try trim function also
UPDATE table_name SET col_name = SUBSTRING(col_name, 7)
It will trim first 7 caracters.

At a very generic level you can try it
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%'
In your case you say these were escaped but since you don't specify how they were escaped, let's say they were escaped to GREATERTHAN
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '<')
WHERE articleItemLIKE '%GREATERTHAN%'
Since your query is actually going to be working inside the string, your WHERE clause doing it's pattern matching is likely to improve any performance - it is actually going to generate more work for the server. Unless you have another WHERE clause member that is going to make this query perform better, you can simply do an update like this:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'GREATERTHAN', '<')
Coming in from a completely different angle:
You can do this when you select the data (not when you do save it)
So instead of :
SELECT MyURLString From MyTable
You can do
SELECT REPLACE (MyURLString, 'GREATERTHAN', '<') as MyURLString From MyTable

This could be done with regular expressions.
SELECT REPLACE(column, '\[[0-9]*\]', '') FROM table;
If MySQL supported them. Unfortunately MySQL does not support regular expressions in REPLACE.
But you could do it with a UDF. Here is a post that might help you.

Related

Change MySQL encoding (regarding hex codes?), such as <fc> to ü

I am not sure how my error is termed, but I think my data show hex codes rather than the actual foreign characters.
To be more precise, I have a MySQL database with data like:
G<e9>rard instead of Gérard, or
M<fc>nster instead of Münster.
Apparently my columns have an utf8_unicode_ci-encoding (according to phpMyAdmin).
Now I wish to convert strings like <e9> into é, either directly in the MySQL-database, or in PHP when the output is being shown.
Apparently others were able to use this response to convert their MySQL-table successfully;
UPDATE db_name SET
column1=convert(cast(convert(column1 using latin1) as binary) using utf8),
column2=convert(cast(convert(column2 using latin1) as binary) using utf8)
However, this doesn't change anything in my case.
So how can I achieve the conversion?
Thank you!
Here's how I would fix this if the special characters are actually sequences of four characters.
First, make sure the table is all converted to utf8mb4:
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8mb4;
Use the REPLACE() function to fix each character one by one.
UPDATE mytable SET
column1 = REPLACE(column1, '<e9>', 'é'),
column2 = REPLACE(column2, '<e9>', 'é');
Be careful if you're editing this SQL query by copy & paste. Make sure you fix column2 in both the left side and right side of the =. Otherwise if you forget one, you could copy the content of column1 into column2, and lose the old content of column2.
Once you're done with é, then do a similar statement for ü:
UPDATE mytable SET
column1 = REPLACE(column1, '<fc>', 'ü'),
column2 = REPLACE(column2, '<fc>', 'ü');
Gradually you will clean up all these hex sequences. You can search the table to see if you have any remaining:
SELECT DISTINCT REGEXP_SUBSTR(column1, '<[[:xdigit:]]{2}>') FROM mytable
WHERE REGEXP_LIKE(column1, '<[[:xdigit:]]{2}>');
(MySQL 8.0 is required for REGEXP_SUBSTR())

sql query to replace backslashes '\\' with '\/'

I know very little about sql queries but I need a query to replace '\\' with '\/'
I have a Wordpress database where there are characters in a long text string that look like this in phpMyAdmin Browse.
'\\uploads\\photos\\'
It needs to be '\/uploads/photos\/'. I want a query to find '\\uploads\\photos\\' so I can make sure the query is working.
I also want another query to permanently replace '\\uploads\\photos\\' with '\/uploads\/photos\/'.
The below query that finds photos returns results, but that's as far as I got.
SELECT *
FROM `mytable`
WHERE `mycolumn` LIKE '%photos%'
Thank you forpas for the excellent solution!
For future readers:
When I migrated my wordpress database from local online, all in one migration plugin missed the paths in wonderplugin gallery that I am using. Thus my requirement for the query.
This double backslashed \ text was in a long string and I was concerned that there were more double backslashes that could get changed. So adding on to the solution provided by forpas, I more accuratly targeted the path text. mytable was actually named wp_nonxenon_wonderplugin_gridgallery, and mycolumn was named data. This is the resultant query that will save me much work in the future.
UPDATE wp_nonxenon_wonderplugin_gridgallery
SET data = REPLACE(data, 'wp-content\\uploads\\photos\\', 'wp-content\/uploads\/photos\/')
WHERE data LIKE '%photos%';
You must escape each backslash with a double backslash:
SELECT REPLACE(mycolumn, '\\\\', '\\/')
FROM mytable
WHERE mycolumn LIKE '%photos%';
Or you can update the table:
UPDATE mytable
SET mycolumn = REPLACE(mycolumn, '\\\\', '\\/')
WHERE mycolumn LIKE '%photos%';
and the column will contain the values as you want them.
See the demo.

Attempting to rename row names using mysql / regex

I am trying to rename rows in a column using mysql and possibly regex or replace?
The names are setup like this...
FL_Miamidade1026295
I need them to look like this...
FLMiami-Dade_1026295
I am thinking the sql statement would look something like this, but not sure how to do the replace part...
UPDATE tableName
WHEN columnName LIKE '%Miamidade%'
Would need regex to somehow change only the middle part of the string
If it is the only pattern you are looking to replace, you can simply use replace.
UPDATE tableName
SET columnName = replace(columnName,'Miamidade','Miami-Dade_')
WHERE columnName LIKE '%Miamidade%'
If Miamidade has to be case-senstitive (meaning miamidade is invalid), use case sensitive collation based on the database settings.
Eg:
WHERE columnName LIKE '%Miamidade%' COLLATE latin1_general_cs
or use regexp binary
WHERE columnName REGEXP BINARY 'Miamidade'

MySql : query to format a specific column in database table

I have one column name phone_number in the database table.Right now the numbers stored in the table are format like ex.+91-852-9689568.I want to format it and just want only digits.
How can i do it in MySql ? I have tried it with using functions like REGEXP but it displays error like function does not exist.And i don't want to use multiple REPLACE.
One of the options is to use mySql substring. (As long as the format doesn't change)
SELECT concat(SUBSTRING(pNo,2,2), SUBSTRING(pNo,5,3), SUBSTRING(pNo,9,7));
if you want to format via projection only, use SELECT, you will only need to use replace twice and no problem with that.
SELECT REPLACE(REPLACE(columnNAme, '-', ''), '+', '')
FROM tableName
otherwise, if you want to update the value permanently, use UPDATE
UPDATE tableName
SET columnName = REPLACE(REPLACE(columnNAme, '-', ''), '+', '')
MySQL does not have a builtin function for pattern-matching and replace.
You'll be better off fetching the whole string back to your application, and then using a more flexible string-manipulation function on it. For instance, preg_replace() in PHP.
Try the following and comment please.
Select dbo.Regex('\d+',pNo);
Select dbo.Regex('[0-9]+',pNo);
Reference on RUBLAR.
So MYSQL is not like Oracle, hence you may just use a USer defined Function to get numbers. This could get you going.

removing characters from field in MS Access database table

Using MS Access 2010.
I have a field in a table that contains windows path names surrounded by quotes, like this
"C:\My Documents\Photos\img1.jpg"
"C:\My Documents\Photos\products\gizmo.jpg"
"C:\My Documents\Photos\img5.jpg"
and so on.
I need to get rid of the quotes so the column looks like this:
C:\My Documents\Photos\img1.jpg
C:\My Documents\Photos\products\gizmo.jpg
C:\My Documents\Photos\img5.jpg
Is there a way to write an update query to do this?
OR a better way to do it altogether?
If you will be doing this from within an Access session, using Access 2000 or later, you can use the Replace() function in an update query to remove the quotes. Remove would mean replace them with an empty string.
UPDATE YourTable
SET path_field = Replace(path_field, '"', '');
If any of those path strings could include quotes within them (yuck!), consider the Mid() function ... ask it to start at the 2nd character (skipping the lead quote), and return the number of characters equivalent to Len(path_field) - 2
UPDATE YourTable
SET path_field = Mid(path_field, 2, Len(path_field) - 2);
Either way, you may want to include a WHERE clause to ignore rows without path_field values.
WHERE Len(path_field) > 0
And if you must do this again when new data is added, use a different WHERE clause to ensure you UPDATE only those rows whose path_field values start and end with quotes.
WHERE path_field Like '"*"'
That was using the * wild card for Access' default ANSI 89 mode. If you will do this from ADO (ANSI 92 mode), use the % wild card.
WHERE path_field Like '"%"'
... or use ALike and the % wild card with either mode.
WHERE path_field ALike '"%"'
The solution with REPLACE already mentioned by others works, but removes ALL quotes, even if they are in the middle of the string.
If you only want to remove quotes at the beginning or at the end, but leave quotes in the middle of the string as they are, you can do it with the following two queries:
Remove first character if it's a quote:
update YourTable
set YourField = right(YourField, len(YourField) - 1)
where left(YourField, 1) = '"'
Remove last character if it's a quote:
update YourTable
set YourTable = left(YourField, len(YourField) - 1)
where right(YourField, 1) = '"'
To make this a permanent change, you might run an update query that looked something like this:
UPDATE [Your Table]
SET [Your Table].[Your Field] = Replace([Your Table].[Your Field],"""","")
This will get rid of all quotes, even if they aren't at the beginning or end. Post back if that's not exactly what you want.
Assuming your column name is MyColumn and table name is MyTable, you can use this sql to update your data to get rid of quotes.
UPDATE MyTable
SET MyColumn = REPLACE(MyColumn,'"','')