SQL Data Update Query About - mysql

I have a table named testlink and it has url and newtarget columns.
I would like to take the string expressions https://domain1.com/ here in the url column and change all the data in the newtarget column to https://domain1.com/search/?q= pulled string expression.
So briefly;
url columns from https://domain1.com/topic1
will be changed to https://domain1.com/search/?q=topic1 in the newtarget column
There are about 6 thousand different topics (lines) available.
Database: Mysql / Phpmyadmin.

use REPLACE
UPDATE testlink
SET newtarget = REPLACE(url,'https://domain1.com/','https://domain1.com/search/?q=')
MySQL REPLACE() replaces all the occurrences of a substring within a
string.
REPLACE(str, find_string, replace_with)

If you want to conditionally change the value, you can use string manipulations:
update t
set url = concat(left(url, length(url) - length(substring_index(url, '/', -1))), 'q=', substring_index(url, '/', -1))
where url like 'https://domain1.com/%';
This uses substring_index() to get the last part of the string (after the last /). It uses left() to get the first part (based on the length of the last part) and then concatenates the values you want.
Of course, test this logic using a SELECT before implementing an UPDATE.

If you're using MySQL 8, then you'd be able to do that with REGEXP_REPLACE.
For your example, this should work :
SELECT REGEXP_REPLACE('https://domain1.com/topic1','(https:\/\/domain1\.com\/)(.+)','$1search/?q=$2')

Related

substring_index does not take exact prefix

I have a table contains strings. Some of them starts with https:// and some starts with http://. I want to extract those that starts with http:// (without s). Please note I do not want to use LIKE statement because of another conflict in my plan to treat this string. So if I have the following items in a column called mycol in mytable:
https://111.com/
https://www.222.com/en-gb/
I make this query:
SELECT `mytable`.`mycol`, substring_index(`mytable`.`mycol`,'http://',-1)
I still get these strings in the results:
https://111.com/
https://www.222.com/en-gb/
Why? since my query is looking for http:// not https://, why do I get results start with https://? In this simple example, it should return nothing as there is no string starts with http://
want to extract the string
use regex. Much more simpler.
SELECT mycol FROM mytable WHERE mycol REGEXP '^http://.+';
You could add check for delimeter:
SELECT `mycol`,
IF(instr(mycol,'http://') > 0, substring_index(`mytable`.`mycol`,'http://',-1),NULL)
FROM mytable;
db<>fiddle demo
When the SUBSTRING_INDEX function cannot find delim string it will return original string instead of NULL.
SELECT substring_index('abc','.',-1)
=>
abc
I want to extract those that starts with http://
If you don't want want to use LIKE you can use left():
select right(columnname, length(columnname) - 7) from tablename
where left(columnname, 7) = 'http://'
You don't need to know the length of the string.
All you want is extract the part of the string after the 1st 7 chars.
The length of this part is:
length(columnname) - 7
So use right().

MySQL for replace with wildcard

I'm trying to write a SQL update to replace a specific xml node with a new string:
UPDATE table
SET Configuration = REPLACE(Configuration,
"<tag>%%ANY_VALUE%%</tag>"
"<tag>NEW_DATA</tag>");
So that
<root><tag>SDADAS</tag></root>
becomes
<root><tag>NEW_DATA</tag></root>
Is there a syntax im missing for this type of request?
Update: MySQL 8.0 has a function REGEX_REPLACE().
Below is my answer from 2014, which still applies to any version of MySQL before 8.0:
REPLACE() does not have any support for wildcards, patterns, regular expressions, etc. REPLACE() only replaces one constant string for another constant string.
You could try something complex, to pick out the leading part of the string and the trailing part of the string:
UPDATE table
SET Configuration = CONCAT(
SUBSTR(Configuration, 1, LOCATE('<tag>', Configuration)+4),
NEW_DATA,
SUBSTR(Configuration, LOCATE('</tag>', Configuration)
)
But this doesn't work for cases when you have multiple occurrences of <tag>.
You may have to fetch the row back into an application, perform string replacement using your favorite language, and post the row back. In other words, a three-step process for each row.

how to extract querystring values from an URL string stored in MySql and save them in new fields?

I have a field "url" in the table "test" of the MySql database, that contains a URL with querystring like
http://www.mydomain.com?q=xxxx&p=yyyy
I have created two fields "query_q" and "query_p" to store the query values in the table, however I can create a SQL to extract each querystring value from "url" field and save them in the new fields?
You can use LOCATE() and SUBSTR() as Barmar said:
See SQL Fiddle.
update t1 set
q=substr(url, locate('?q=',url)+3, locate('&p=',url)-locate('?q=',url)-3),
p=substr(url, locate('&p=',url)+3, length(url)-locate('&p=',url)-2);
Of course, for this to work, q has to be the first parameter, preceeded by '?' and 'p' the second. But if not, you can figure it out ;)
If you just need a quick & dirty solution, this might help:
UPDATE urls
SET
query_q = SUBSTRING_INDEX(SUBSTRING_INDEX(url, '?q=', -1), '&p=', 1),
query_p = SUBSTRING_INDEX(url, '&p=', -1)
Please see fiddle here.

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.

Using mysql Regexp in a select statement not the where clause

So what i want to do is use a Regex to select only certain parts of data from a column
Example :
SELECT URL REGEXP 'http://' from websitelist --(website list is a list of URL's)
If I run that against the table it returns 1 foreach row in which 'htt://' was found, what I want is to return the string that matches the regexp
The REGEXP operator performs a match and returns 0 or 1 based on whether the string matched the expression or not. It does not provide a way to extract the matched portion. I don't think that there is any way to extract the matched portion.
You could use just use string functions if it's as simple as your example - just removing http://
SELECT REPLACE(URL, 'http://', '') AS url FROM websitelist;
Probably faster as there is overhead for the REGEX engine.