I have strings that look like this:
#N05/3835412983/academy-of-sciences.html
Where 3835412983 and academy-of-sciences.html will be different throughout the database table called wp_4_posts in a column called post_content.
Is there a wildcard search that I can do to preserve the unique id between the two / and strip *.htm*? Everything to the left of the #05 cannot change. The numbers between the / need to be preserved. And the item after the second / can be stripped entirely.
Doing it manually and there's hundreds that need this done to.
For the above, the end result would be:
#N05/3835412983/
And that result would be written back into the database.
If you want to extract the middle element, then you can use substring_index():
select substring_index(substring_index(str, '/', 2), '/', -1)
This extracts the second element.
Based on the edit, you seem to want:
select concat(substring_index(str, '/', 2), '/')
If you want to update the value, then:
update t
set str = concat(substring_index(str, '/', 2), '/');
update specific string then should use replace() function
Syntax:-
UPDATE table_name
SET column_name =
REPLACE(column_name,'OLD_STRING','NEW_STRING')
WHERE column_name = 'your condtition';
in your case:-
UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'#N05/3835412983/academy-of-sciences.html','#N05/3835412983/');
if you want result based on condition
UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'#N05/3835412983/academy-of-sciences.html','#N05/3835412983/')
WHERE column_name = "values";
Related
I have a column with links in my table. Some links have a string like /?source=rss&....
So I want to remove this string /?source=rss and what comes after it.
So if the link looks like this https://website.com/?source=ress&id=123&name=john
It should be https://website.com
I searched about it but only found replace and remove functions, And they won't work as the string is variable and not the same in every link.
I found this UPDATE MyTable SET column = REPLACE(column, ' ', '') WHERE column LIKE '%%'
The database is MYSQL and I'm using SQL queries there
Use substring_index():
UPDATE MyTable
SET column = SUBSTRING_INDEX(column, '/?', 1)
WHERE column LIKE '%/?%';
I am trying to remove everything before the third / in a column. For example: If there is a URL in the coloumn such as
http://www.example.com/example1/example2?=testest123
I would like to remove everything (not including the thrid slash) so i will be left with something like this
/example1/example2?=testest123
I have tried using this but it only removes everything from first "/" and i can't work out how to get it to count to the third then remove.
update table
set column2 = substring(column1, instr(column1, '/') + 1);
Thanks.
To start, you can use the SUBSTRING_INDEX function to get the characters of the string leading up to the third (or fourth slash, in your example) like this:
SELECT SUBSTRING_INDEX(val, '/', 4)
FROM myTable;
You can use the REPLACE() function to remove that substring by replacing it with an empty string, like this:
SELECT REPLACE(val, SUBSTRING_INDEX(val, '/', 4), '')
FROM myTable;
Now, to update your table, simple rewrite the query to set the value to the one above:
UPDATE myTable
SET val = REPLACE(val, SUBSTRING_INDEX(val, '/', 4), '');
NOTE that if there are less than four occurrences of a forward slash, SUBSTRING_INDEX will return the entire string, and therefore completely replacing the entire string by an empty value so you should be very careful when preforming this update.
Here is an SQL Fiddle example with your sample text, and one that I wrote with fewer slashes to demonstrate the last point.
That's a bit messy, but you can try this :
UPDATE table
SET column2 = SUBSTRING(column1,
LOCATE('/', column1,
LOCATE('/', column1,
LOCATE('/', column1))+1)+1)
Hi i need help with a MySQL query.
Is it possible to do some wildcard replace for a string like the example below?
String: example.com/folderone/some/path/to/file.pdf
String: example.com/foldertwo/some/path/to/file.pdf
Replace: newsite.com/some/path/to/file.pdf
newsite.com/some/path/to/file.pdf
newsite.com/some/path/to/file.pdf
To remove the folder and change the domain but keep the path. In this case each folder have a different name with a different length.
something like:
update TABLE set COLUMN = replace(COLUMN, 'example.com/%/', 'newsite.com/');
Using SUBSTRING_INDEX:
UPDATE table1
SET column1 = REPLACE(
column1,
SUBSTRING_INDEX(column1, '/', 2),
'newsite.com'
)
WHERE column1 LIKE 'example.com/%/'
This should honour your subfolder structure.
I am trying to make an update on a DB replacing old characters for new ones, my problem is that at the middle of that string I have some characters I have to remove but I dont know the length or the type, I just know when the sub string starts and when it ends, for exmaple
my-string=XXXXXXXX-endstring
so, Id like to make it like this
new-string-endstring
I know my-string value, but dont know the sub string in the middle XXXXX
is there any way to do it?
Thanks.
Update fields which begin with my-string- AND end with -endstring:
UPDATE table_name
SET column_name = 'new-string-endstring'
WHERE column_name LIKE 'my-string-%'
AND column_name LIKE '%-endstring';
If you use PHP you could do the following:
1) Make a SELECT of all rows that have the my-string text inside the column value
SELECT * FROM table_name WHERE column_name LIKE '%my-string%'
the % acts as a wildcard
2) Loop through all these result rows and use PHP's str_replace to replace my-string for new_string, and then use MySQL's UPDATE to update the new value of the column in the DB
You can use SUBSTRING_INDEX to get the required string and use CONCAT to append new string.
Update TableA
Set columnA = CONCAT( 'newstring' , SUBSTRING_INDEX(columnA, '-', -1) )
Imagine that you have a column with a bunch of fields like this:
ab
abc
abcd
...
I would like to transform these fields using a single query to something like this:
a_b
ab_c
abc_d
...
That is, inserting an underscore just before the last letter.
I could do it in a one by one basis:
update test set name = 'a_b' where name ='ab';
update test set name = 'ab_c' where name ='abc';
update test set name = 'abc_d' where name ='abcd';
But being a high number of fields to update, I figure there must be a better way to do this.
Try this query -
UPDATE test SET name = INSERT(name, LENGTH(name), 0, '_');
Use some of the string manipulation functions, something like:
SET fieldname = CONCAT(LEFT(fieldname, LENGTH(fieldname)-1),"_",RIGHT(fieldname,1))
You could use the SUBSTRING AND LENGTH functions:
UPDATE test
SET name = SUBSTRING(name, 0, LENGTH(name) - 1) + '_' + SUBSTRING(name, -1)