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));
Related
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.
I'm using Wordpress and have a load of custom fields where I need to do a string replace on. Basically I have a price field which is in the example format:
from 113.800
I need to remove the word 'from', the space after it and the dot between the number. All the fields are in this format, how can I use a MySQL replace function to do this?
Thanks
REPLACE() doesn't support regular expression matching, so you'll have to do it in a more clumsy way:
UPDATE wp_sometable
SET price_field = REPLACE(REPLACE(price_field, 'from ', ''), '.', '');
I'm just getting used to MySQL, I've come from a SQL Server background...
This SQL query builds an address how it should in SQL Server, how can I adapt it to use within MySQL. When I run it in SQL Server it displays all the data within each field, when run in MySQL it just shows me the first field.
Why would this be, what should I do different in MySQL?
SELECT COALESCE(House, '') + ' ' + COALESCE(StreetName, '') + ' ' + COALESCE(TownCity, '') + ' ' + COALESCE(Postcode, '') AS Display
FROM MyTable
WHERE Postcode LIKE '%A1 2AB%'
Use the concat() function:
SELECT concat(COALESCE(House, ''), ' ', COALESCE(StreetName, ''), ' ',
COALESCE(TownCity, ''), ' ', COALESCE(Postcode, '')
) AS Display
FROM MyTable
WHERE Postcode LIKE '%A1 2AB%';
You can also do this with concat_ws(). This eliminates the need for all the spaces:
SELECT concat_ws(COALESCE(House, ''), COALESCE(StreetName, ''),
COALESCE(TownCity, ''), COALESCE(Postcode, '')
) AS Display
FROM MyTable
WHERE Postcode LIKE '%A1 2AB%';
What happens in MySQL is that the + does just what you expect: it adds numbers. A string that contains a number is converted to a number automatically, with silent errors for strings that have no numbers. In practice, this means that a string that starts with a non-digit (and non-decimal point) is converted to a 0.
So, house, which presumably usually numeric, is converted to a number just fine. All the other strings are converted to numbers but become zero and the house number is not changed. You would have gotten much different results if your post codes were American-style zip codes (which are typically numeric).
EDIT:
As #fthiella points out, the coalesce() is not necessary for concat_ws(). The two statements would do different things, because NULLs in the original query result in repeated separators. NULLs in the concat_ws() version would have only a single separator (which might be desirable).
However, I would tend to keep the coalesce() anyway. The behavior of concat() and concat_ws() varies in this regard. concat() returns NULL if any of its arguments is NULL. concat_ws() skips NULL arguments after the initial separator. Who can remember that distinction? It sounds like a recipe for confusion in production code. So, I would also use coalesce() even though it is optional.
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?
I have some sentences in string in MySQL. And I need to replace substrings such as 'My' to 'my' if this word not first in sentence. How I can doing this?
CHAR, REPLACE, REPEAT, etc. I'd recommend reading mySQL ref: http://dev.mysql.com/doc/refman/5.5/en/string-functions.html
If you just want to replace several words, you can replace them, using this approach:
UPDATE str_test SET str = REPLACE(str, ' My', ' my')
fiddle
As the words inside the text will be preceded by space. But if you want a regexp replace, it will be a more difficult task:
How to count words in MySQL / regular expression replacer?
https://dba.stackexchange.com/questions/15250/how-to-do-a-case-sensitive-search-in-where-clause
MySql support for string is very limited. A quick solution would be to use something like this:
SELECT
CONCAT(
LEFT(col, 1),
REPLACE(SUBSTRING(col, 2), 'My', 'my')
)
Please see fiddle here. This will replace all the strings My to my, except the first one.
Or maybe this:
SELECT
col,
RTRIM(REPLACE(CONCAT(col, ' '), ' My ', ' my '))
FROM
yourtable
that will replace all whole words except the first one.