Lets say that you have the following stored in table:
{2:22}{4:5}{34:4}
I what to delete {4:5} from this string but the system dosent know what the number after the ":" is just the first one. The query looks something like this:
UPDATE tbl SET this = REPLACE(this,'{4:??}','') WHERE id = 1;
What do i need to put in ?? place to return the following result?
{2:22}{34:4}
Here's one way to do it using LEFT, SUBSTRING, LOCATE and REPLACE:
update yourtable
set yourcolumn =
replace(yourcolumn,
Left(
Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)),
Locate('}',Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)))),
'')
SQL Fiddle Demo
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 need to write a query that appends a "+" on the front of every p2_number meta_key that doesn't already begin with a "+". The name of the table is "wp_4_postmeta". I attached an image of the database so you can see what I'm talking about. http://mmw-file-sharing.s3.amazonaws.com/2015/Screen%20Shot%202015-07-30%20at%204.12.25%20PM.png
UPDATE aTable
SET someField = CONCAT('+', someField)
WHERE someField NOT LIKE '+%'
;
If someField is also indexed, the query should be fairly quick as well.
Update table set column = concat("+", column) where column not like "+%"
Should do it
You can check using substr if the first character is a +. If so, return just the field value, else prepend a +.
select
case when substr(p2_number, 1, 1) = '+' then
p2_number
else
concat('+' p2_number)
end as p2_numberplus
from
wp_4_postmeta
Or do you mean actually updating the table data? In that case, Uueerdo's answer is the one you want.
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) )
I'm trying to find certain text "catid=18" in a string, where each string is different except for this.
I've used this query below before, except it only seems to work if you know the entire string.
update TABLE_NAME
set FIELD_NAME = replace(FIELD_NAME, 'findthis', 'replacewiththis');
Not sure if that is what you want. But it will return 1 if catid=any_num is found and 0 if not:
select 'some_text catid=18 some_text' REGEXP 'catid=[0-9]+'
Maybe you need:
update TABLE_NAME
set FIELD_NAME = 'goodvalue'
WHERE FIELD_NAME = 'badvalue';
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)