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 '%/?%';
Related
i have found some wrong text in 120 record in a table so i have in a varchar field:
'Name rubbish rubbish2 more rubbish'
i want to keep 'Name' en remove all after Name
i have tried this :
SELECT REPLACE(field_name, 'rubbish','') as test
from table
where field_name like '%rubbish%'
but this will ofcourse remove only 'rubbish' not the rest.
I think ther must be a way to remove everything after 5 digits!?
Txs
To remove everything after the first space character:
update mytable set
field_name = substr(field_name, 1, instr(field_name, ' '))
where field_name like '%rubbish%';
See SQLFiddle.
In MySQL, if you want to keep everything before the first space, then you can use substring_index():
update t
set col = substring_index(col, ' ', 1)
where col like '% %';
If you have some set pattern, such as the string 'rubbish', then you can use that. So, this keeps everything before "rubbish":
update t
set col = substring_index(col, 'rubbish', 1)
where col like '%rubbish%';
You can also use this logic in a SELECT statement:
select substring_index(col, 'rubbish', 1)
. . .
If the string does not contain "rubbish", then everything is returned.
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 have a table where one field named "version" contains the string "MyProgram nnnnnn".
I now wish to replace these strings so that they are only "nnnnnn", thereby remove the prepending "MyProgram ".
Is this possible, and if so, how would I do this?
If the pattern is "MyProgram nnnnnn" maning string like VB 1.3, Mysql 5.6, PHP 5.4 etc then you can do the following
update tablename
set col = substring_index(col,' ',-1)
Use Replace function,
SELECT VERSION,
REPLACE(VERSION,'MyProgram nnnnnn','nnnnnn')
FROM FROM tablename
Check this Manual
SELECT REPLACE(VERSION, 'MyProgram ', '')
FROM tablename
You can use the Replace() function for MySQL
update table set columnname = REPLACE(columnname, 'MyProgram ', '');
You want:
I now wish to replace these strings so that they are only "nnnnnn"
Shortest solution:
SELECT 'nnnnnn'
Specify your pattern of the content of this column!
SELECT VERSION,
REPLACE(VERSION,'MyProgram nnnnnn','nnnnnn')
FROM FROM tablename
Regards,
Praveen Nelge
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
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';