How do I safely turn a name into a 'sanitised alias' in a MySQL view? - mysql

I've got a table with real names, e.g. "Matt Smith", but I need to convert them to be used as URL paths in a web app. "Matt Smith" would become "matt-smith", "Danny O'Brien" would become "danny-obrien", etc.
I need to lowercase, strip special characters, and replace spaces with dashes.
I know I can do this to replace spaces:
SELECT REPLACE( table.field, ' ', '-' ) FROM table;
And I know there's a LOWER() function as well.
What I don't know is:
How to strip special characters
How to combine all three into one SQL query that actually works
If it's possible to make it work automatically when I create the view, and for it to keep working as new rows are added via the web app

If you are using MySQL 8+, then REGEXP_REPLACE is one option:
SELECT
LOWER(REPLACE(REGEXP_REPLACE(field, '[^A-Za-z0-9]', ''), ' ', '-')) AS alias
FROM yourTable;
The innermost call to REGEXP_REPLACE strips off all non alphanumeric characters.

Related

Multiple string replace in apache drill using REGEXP_REPLACE sql method

I am using drill sql query on json data. But one of my json field seems to have few characters e.g. '\n' & '^' etc which I want to replace on the fly.
Currently, I am calling REGEXP_REPLACE twice as below -
SELECT REGEXP_REPLACE(REGEXP_REPLACE('aaaa\nbbbb^cccc', '\\n', ' '), '\^', ' ') FROM (VALUES(1));
How can I do that using REGEXP_REPLACE method only once?
The below must work -
SELECT REGEXP_REPLACE('aaaa\nbbbb^cccc', '\\n|\^', ' ') FROM (VALUES(1));
But note that in this case, the character you are replacing with, will be same for all. If you have to replace with different characters then you would need to go with your approach only as below -
SELECT REGEXP_REPLACE(REGEXP_REPLACE('aaaa\nbbbb^cccc', '\\n', 'X'), '\^', 'Y') FROM (VALUES(1));

Search within Mysql with special chars

If I have a table files and it has a column title, and some of the titles are in this format:
google: and facebook
stack: overflow
Now I'm trying to add search functionality in my app, which executes a LIKE '%word%' query. But if people search google and facebook it doesn't find anything, unless they specifically search for google: and facebook.
I know what LIKE does and why it doesn't give the results I'm looking for, I'm just asking if there's a way to search in mysql table and ignoring special chars like : ' - . , " etc.
Thanks.
Use REPLACE function prior to comparison.
... REPLACE( fieldname, ':', '') LIKE %word%
Alternative is to replace all spaces in the search string with '%' or you explode the search string (space as separater) and connect the different parts with an OR statement.
You can work with regular expressions
MYSQL Manual

Cleaning out a field of Phone numbers in mySql

In not a database guy but: I have mixed up data in a mySql database that I inherited.
Some Phone numbers are formatted (512) 555-1212 (call it dirty)
Others 5125551212 (Call it clean)
I need a sqlstamet that says
UPDATE table_name
SET Phone="clean'(Some sort of cleaning code - regex?)
WHERE Phone='Dirty'
Unfortunately there's no regex replace/update in MySQL. If it's just parentheses and dashes and spaces then some nested REPLACE calls will do the trick:
UPDATE table_name
SET Phone = REPLACE(REPLACE(REPLACE(REPLACE(Phone, '-', ''), ')', ''), '(', ''), ' ', '')
To my knowledge you can't run a regexp to replace data during the update process. Only during the SELECT statement.
Your best bet is to use a scripting language that you're familiar with and read the table and change it that way. Basically by retrieving all the entries. Then using a string replace to match a simple regexp such as [^\d]* and remove those characters. Then update the table with the new value.
Also, see this answer:
How to do a regular expression replace in MySQL?

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.

mysql - replace ' with single quote in a field

I'm not exactly sure what happened because I migrated from One server to another of the same spec and SQL...
Still in comments and titles the new database shows the characters ' instead of '
and I was wondering if I could ask for help in either replacing ' with '
or if it was simpler just deleting '
Thanks so much...
Steff
You could use MySQL's REPLACE method (look here):
UPDATE
Changed the statement to reflect the OP's naming:
UPDATE database1.vb_ppgal_albums
SET pp_photos = REPLACE(pp_photos, ''', '\'')
Good luck.
The following is the coding that I use to update double-quote in MySQL. I use the REPLACE function. The first parameter is the field_name that I want to have searched, the second is the escaping of the double quote (\") as the search string, followed by escaping of the escape character (\) followed by the double quote, which will insert into the field name (\"). In the table I will now have a measurement of '1/2\"' instead of '1/2"', which was my goal. I hope this helps. (PS, the Where clause is for show, you may not need it.)
UPDATE `table_name`
SET
`field_name` = REPLACE(`field_name`, '\"', '\\"')
WHERE `Id` > 125