Is it possible to replace multiple things at once in MySQL?
It feels kinda clumsy doing this
REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( myField, 'å', 'a' ) , 'Å', 'A' ) , 'Ø', 'O' ) , 'ø', 'o' ) , 'æ', 'a' ) , 'Æ', 'A' )
Short answer, no. There's really no need to elaborate :)
You can create your own user defined function to do this, but it's probably something I would put in a layer other than the database.
The database is optimised for the stuff it needs to do, data extraction/sorting/grouping and so on.
While it can do some manipulation of data within a column, that's not really its primary purpose (leave that to the presentation or other layers).
If you must do it within the DBMS, consider doing it with a separate column holding the modified data, and having that maintained with an insert/update trigger. This will at least ensure the cost is incurred only when the data changes, rather than every time you look at it. In other words, it will amortise the cost across all selects.
Related
How do I run a query to convert price column from text to numbers(BIGINT) in a database? Is it possible to do such processing using only SQL queries?
Price
————-
2 millions 5 hundreds thousands
52 thousands
3 hundreds 25 thousands
10 millions 30 thousands
UPDATE
Like what you guys commented, I guess this task is better done with other language instead of SQL.
Can you do it? Sure you can but you really shouldn't. Here's an example (abbreviated) of what it might look like:
SELECT CAST(CONCAT(
CASE WHEN priceData.millions IS NOT NULL
THEN LPAD(priceData.millions, 3, '0')
ELSE '000',
CASE WHEN priceData.thousands IS NOT NULL
THEN LPAD(priceData.thousands, 3, '0')
ELSE '000'
-- additional branches for hundreds, tens, whatever else you want to process
) as BIGINT) price
FROM (
SELECT
REPLACE(
REGEX_SUBSTR(table.price, '[0-9]* millions'),
' millions',
'') as millions,
REPLACE(
REGEX_SUBSTR(table.price, '[0-9]* thousands'),
' thousands',
'') as thousands,
REPLACE(
REGEX_SUBSTR(table.price, '[0-9]* hundreds'),
' hundreds',
'') as hundreds'
-- additional branches for hundreds, tens, whatever else you want to process
FROM table
) as priceData
This is a minimal proof of concept, but it will require a lot of building out before it works. It also makes a lot of assumptions about your data, will be insanely slow, and will make whoever comes to maintain your code want to gouge their eyes out. I mainly provided it to show you just how sad it will make you to put it all together.
The real solution would be to store the numbers as BIGINT before the data gets to the db at all. If you can't do that, I would do this kind of processing programmatically in whatever it is that's querying your db.
I would like to search inside 2 columns for parts of a string. It's a backwards way of searching so I doubt it's possible.
I think part of it would have something to do with:
REPLACE( event_name, ' ', '')
and
REPLACE( venue_name, ' ', '')
to get rid of the space.
I also thought REGEX might come into it.
Absolutely no idea where to start! PSUEDOCODE might be:
CONCAT(
event_name = REPLACE( part of :search, ' ', '')
,
venue_name = REPLACE( part of :search, ' ', '')
)
= :search
If I used noddybobstheatre as the search term I want to search the column event_name for part of it and venue_name for another part and when the 2 parts are put together they equal the search term.
event_name = 'noddy'
venue_name = 'bobs theatre'
Like I said, this might be a crazy ask...
Reading what you need I thought that the like statement should use the column and not the search value.
here is my reproduction of what I understood from your problem
CREATE TABLE movies
(`word1` varchar(5), `word2` varchar(7))
;
INSERT INTO movies
(`word1`, `word2`)
VALUES
('micko', 'to'),
('he', 'llbrane'),
('mick', 'oto'),
('hell', 'brane')
;
then here is the sql statement that I used
select * from movies where 'mickoto' like CONCAT(Replace(word1,' ',''),'%') and 'mickoto' like CONCAT('%',Replace(word2,' ',''))
all you have is to adapt this to your context
here is the fiddle execution
use this,
concat(replace(event_name,' ',''),replace(venue_name,' ',''), )=:search
I have a situation:
I have a database (MySQL) which contains products and their codes like this
BLACK SUGAR BS 709
HOT SAUCE AX889/9
TOMY 8861
I got an excel spreadsheet which I converted to CSV, this contains prices for the products. Its structure consists in 2 columns, code and price, like this:
BS709 23.00
AX 889 /9 10.89
8861 1.69
I made a script to update the products prices by searching in the database for the respective product code, using a FOREACH and %LIKE% query.
FOREACH row in CSV, search the database using "WHERE product_code LIKE %code%.
This is offcourse a primitive and not so succesfull way of updating the prices, because the codes in CSV are not an exact match (in syntax) of those in the database so if I have two products in the DB containing BS709 (BS70923) in their code I get multiple matches.
Is there a better way of doing this ?
You could trim the columns of spaces and other characters using MySQL replace() before comparing. This will return all exact matches, regardless of any spaces contained.
SELECT * FROM table WHERE REPLACE( product_code, ' ', '' ) LIKE 'code'
Given your examples, I would recommend removing all spaces from both, and then just looking for when the beginning or end of a code matches exactly:
where replace(e.code, ' ', '') like concat(replace(db.code, ' ', ''), '%') or
replace(e.code, ' ', '') like concat('%', replace(db.code, ' ', '')) or
replace(db.code, ' ', '') like concat(replace(e.code, ' ', ''), '%') or
replace(db.code, ' ', '') like concat('%', replace(e.code, ' ', ''));
This may not work for the specific case when one code is a prefix of another.
In any case, if the product codes in a spreadsheet are different from the product codes in the database, I think you have bigger problems. If you cannot really fix the spreadsheets, I would recommend that you manually/semi-automatically create a synonyms table in the database. This would have the Excel product code in one column and the correct product code in the other. Then you can do the lookup just by joining this together.
Yes. That is work. But probably less work than struggling with this problem and getting poor results that have to be repeatedly updated.
I am using the following query to select duplicate phone numbers from a table.
SELECT id, REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( phone, "+", '' ) , ")", '' ) , "(", '' ) , "-", '' ) , ' ', '' ) AS strippedPhone
FROM `customers`
GROUP BY strippedPhone
HAVING count( strippedPhone ) >1
LIMIT 0 , 300
It looks ugly and does not consider the possibilities of alphanumeric character on the field having the phone number.
Any better ways?
This StackOverflow post MySQL strip non-numeric characters to compare has a solution that uses the NumericOnly function.
http://venerableagents.wordpress.com/2011/01/29/mysql-numeric-functions/
In my opinion you would be to normalize the phone numbers before you stored them in the database. Any sql query that is able to compare phone numbers regardless of format is going to be highly inefficient. Where as if you if you reformat the phone numbers before inserting them into the database checking for duplicate numbers is trivial.
If you want to do this on an existing database you will have to normalize the data in the database first but that only has to be done once.
I have troubles with doing such sql query for mysql db:
I need to update field A in my db, but also i have B field, which contains much data, for example:
ASIAN HORSE 70з рус 600A (261x175x220)
or
Бэрен polar 55/59з (555112) 480A (242x175x190)
i must fetch 70з and set it in field A, and 55/59з same (but for another record).
But how can i search in B field something what end's with з but is word (not all data as % before з)
I know, that it could sound like homework... but i real don't know ho to select only word with some end...
The MySQL function substring_index can be used to select pieces of a string delimited by something. For example this picks out the third "word" from MyColumn:
select substring_index(substring_index(MyColumn, ' ', 3), ' ', -1) from MyTable
(70з is the third "word" in ASIAN HORSE 70з рус 600A (261x175x220).)
Update If instead of the third word you are looking for the "word" that ends with 'з', you can use:
select substring_index(substring_index(MyColumn, 'з', 1), ' ', -1) from MyTable
This will consider 'з' as the delimiter though, and removes it from the result. You can add it back with concat:
select concat(substring_index(substring_index(MyColumn, 'з', 1), ' ', -1), 'з') from MyTable
If you are trying to parse the field so the third value always goes into a particular field, then you have a hard problem and probably want to create a user-defined function.
However, if you just want to see if 70з is present and set another field, then this should work:
update t
set B = '70з'
where A like '% 70з' or A like '% 70з %' or A like '70з %' or A = '70з'
This uses spaces to define the word boundaries and considers whether the string is at the beginning, in the middle, at the end, or the entire value in A.