SQL - How do I replace/modify strings in multiple rows? - mysql

I have a lot of rows in database where column "info" is ie. "Size, inch: 12x13<br>Material: paper<br>Amount: 100pcs". Now I need to find/select all rows that has this string part "Size, inch:" and replace/fix those rows' colums to ie. "Size: 12x13 inch<br> Material: paper<br>Amount: 100pcs ".
How in the world I write a sql statement for this? Do I need some magic regexp for sql? How do I replace and modify parts of a string in multiple rows?
EDIT: The numbers can be anything (ie. 12x13 or 44x55 or 77x88x99 etc.) So there would have to be some kind of wildcard for the numbers, perhaps?
I need to change "Size, inch: 'anynumbershere' 'anythingafter the numbers'" to "Size: 'anynumbershere' inch 'anythingafter'".

update mytable set info=REPLACE ( info , 'Size, inch: 12x13 ', 'Size: 12x13 inch' )
where info like '%inch:12X13%'

Perhaps this will work:
update table t
info = replace(replace(info, 'Size inch:', 'Size:'), '<br>Material', ' inch<br>Material')
where info like 'Size inch:%<br>Material%';
Note: this assumes that 'Size inch' and '<br>Material' only appear once in each string.

Related

Google Sheet Query and ImportHTML Function show numerical values as text

I'm importing some data using the formula below but the numerical values appear as =1599 (for example) and are being treated as text (ie cannot use them in a formula).Does anyone know how to substitute the "=" to "" in the table? The numerical values are in column H.
={QUERY(IMPORTHTML("https://1234567.website.com","table",0), "where Col1 is not null",1)}
I tried wrapping in:
SUBSTITUTE( ... ,"=","")
ARRAYFORMULA(SUBSTITUTE( ... , "=","")
TO_PURE_NUMBER(
Nothing works. Is there a way to apply one of these solutions only to the columns with numerical values?
Try iferror() and regexextract(), like this:
=arrayformula(
lambda(
data,
iferror(value(regexextract(data, "[-.\d%]+")), data)
)(
importhtml("https://1234567.website.com", "table", 0)
)
)

MySql - Select some characters after certain word and add it to another column

I have table which include 2 column: title and param, the values are like the following:
-title: Teaching
-params:
{"ufield926":"34","ufield927":"Sud","ufield928":"Ara","ufield929":"Mecca",
"ufield930":"1\/1\/1983","ufield933":"011","ufield934":"Mub",
"ufield943":"SU\/HI\/14","ufield944":"Average","ufield946":"Female"}
I want to extract the code after "ufield943": which is SU\/HI\/14 only and concatenate it with the value in title column to be like the following:
Teaching (SU\/HI\/14)
Try this query:
select *,
substring(
params,
locate('ufield943', params) + 12,
locate('ufield944', params) - locate('ufield943', params) - 15
)
from tbl;
But I assumed that ufield944 occurs directly after ufield943.
Demo
The value in params is JSON, so your best solution might be to look at MySQL JSON support.
I'm not familiar with MySQL JSON support, if that doesn't work for you I would look at MySQL's REGEXP_SUBSTR and REGEXP_REPLACE to do this:
SELECT
title
REGEXP_REPLACE(
REGEXP_SUBSTR(params, '"ufield943"[[:space:]]*:[[:space:]]*"([^"\\]|\\.)+'),
'^"ufield943"[[:space:]]*:[[:space:]]*"', '')
FROM MyTable
The regex finds the value regardless of where it is in the json and allows for whitespace in the column (like "ufield943" : "SU\/HI\/14").

Replace template smart tags <<tag>> to [tag] in mysql

I have an table name templateType, It has column name Template_Text.
The Template have many smart tags <> to [tag] using mysql, and I need to replace << to [ and >> with ].
Edit from OP's comments:
It is an template with large text and having multiple smart tags. As example : " I <<Fname>> <<Lname>>, <<UserId>> <<Designation>> of xyz organization, Proud to announce...."
Here I need to replace these << with [ and >> with ], so it will look like
" [Fname] [Lname], [UserId] ...."
Based on your comments, your MySQL version does not support Regex_Replace() function. So, a generic solution is not feasible.
Assuming that your string does not contain additional << and >>, other than following the <<%>> format, we can use Replace() function.
I have also added a WHERE condition, so that we pick only those rows which match our given substring criteria.
Update templateType
SET Template_Text = REPLACE(REPLACE(Template_Text, '<<', '['), '>>', ']')
WHERE Template_Text LIKE '%<<%>>%'
In case the problem is further complex, you may get some ideas from this answer: https://stackoverflow.com/a/53286571/2469308
A couple of replace calls should work:
SELECT REPLACE(REPLACE(template_text, '<<', '['), '>>', '])
FROM template_type

How to parse a string in MySQL

So the problem is I have a column that contains a snapshot:
<p>
<t8>xx</t8>
<s7>321</s7>
<s1>6</s1>
<s2>27</s2>
<s4>73</s4>
<t1>noemail#noemail.com</t1>
<t2>xxxxx</t2>
<t3>xxxxxx</t3>
<t11>xxxxxxxx</t11>
<t6>xxxxxxxx</t6>
<t7>12345</t7>
<t9>1234567890</t9>
</p>
I need to parse this string in MySQL so that I can count the number of times that noemail.com occurs. I am not familiar with parsing so if you could please explain the best you can.
You can do it by removing searched substring and comparing the lengths. For example :
set #str = '<p>
<t8>xx</t8>
<s7>321</s7>
<s1>6</s1>
<s2>27</s2>
<s4>73</s4>
<t1>noemail#noemail.com</t1>
<t2>xxxxx</t2>
<t3>xxxxxx</t3>
<t11>xxxxxxxx</t11>
<t6>xxxxxxxx</t6>
<t7>12345</t7>
<t9>1234567890</t9>
</p>';
set #find = 'noemail#noemail.com';
select (length(#str) - length(replace(#str, #find, '')))/length(#find) AS NumberOfTimesEmailAppears;
I think there is sadly no more elegant solution (note that a database system is not designed to parse strings : this is most the job of a scripting language)

How to filter a name from a sentence in database?

I want to query string from a field from my database. For example: Field "Address", value "Toul Kork District, Phnom Penh". Want I want to get is only "Phnom Penh". I know that my sql is not allow to select only this string from the field. So, what is the good way to do that?
This may help you :
SELECT SUBSTR(Address,
LOCATE(Value,Address),
LENGTH(Address) - LENGTH(Value));
POSITION function is synonim for LOCATE. You can use POSITION function as well :
SELECT SUBSTR(Address,
LOCATE(Value IN Address),
LENGTH(Address) - LENGTH(Value));
In static variable it's looks like :
SELECT SUBSTR('Toul Kork District,Phnom Penh',
LOCATE('Phnom Penh','Toul Kork District,Phnom Penh'),
LENGTH('Toul Kork District,Phnom Penh') - LENGTH('Phnom Penh'));
Result : Phnom Penh