Remove some text from a column values - mysql

I have a column in my table named title_id with some title ids. Each title id is stored like
title_1
title_2
title_3
where 1, 2, 3 are the actual ids of the titles.
I want to remove the " title_ " text from all the records. I have around 5000 records so I can't edit them manually.
How can I do it with a query.
Thanks in advance

Update table_name set `title_id` = REPLACE(`title_id`,'title_','');
I didnt' tested it . Please check

UPDATE table_name SET title_id = REPLACE(title_id, 'title_','')

Check out the REPLACE function. You can do something like:
UPDATE table
SET title_id = REPLACE(title_id, 'title_', '');
(Ah, and be sure to first test your UPDATE query by running a SELECT query!)

Try something like
update table_name set id=substring(id, from length('title_'))
where id like 'title%';
I didn't test this, because I have no MySQL DB available here. The syntax for the substring function is from the MySQL docs.

Related

How to search for string from one column in other column and delete?

Let's say I have two columns in my MySQL database called first and second.
I want to search in second for the value of first an delete it.
Example:
First row: first is: test, second is: Hello, this is a test!
Expected result: second is: Hello, this is a !
I think it shouldn't be difficult but I don't know how to do.
Can anybody help me?
For querying, this should do it.
SELECT
REPLACE(second, first, '')
FROM your_table;
For updating:
UPDATE your_table SET second = REPLACE(second, first, '');
As mentioned by #Gordon in the comments below, add a where clause to do the update on the required rows only:
UPDATE your_table SET second = REPLACE(second, first, '')
WHERE second like concat('%', first, '%');

MySQL phpMyAdmin replace all like pattern in multiple tables

I am trying to look for a way to replace all occurrences of a particular pattern in my database across many tables and columns, for this I need to create some way to do this, it does not need to be done by a script, just some SQL code that will do this.
For example, I want to replace all occurrences of 'v2' with 'www' but have no idea how to do this.
I am not looking for a tutorial, just a bit of guidance on what to do and how to script the SQL needed.
How do I go about doing this?
Just to guide you in a direction. You can use the replace function:
UPDATE MyTable
SET StringColumn = REPLACE (StringColumn, 'SearchForThis', 'ReplaceWithThis')
WHERE SomeOtherColumn LIKE '%PATTERN%';
In an earlier post there is more information:
How can I use mySQL replace() to replace strings in multiple records?
You can use a select CONCAT(...) from information_schema.columns to generate an update query for every table-column combination, where ... is a combination of the strings used in an update query, and column names of the information_schema.columns database.
For example:
select CONCAT("UPDATE ", TABLE_NAME, " SET ", COLUMN_NAME, "=REPLACE(",COLUMN_NAME,"'[string-to-find]'","'[string-that-will-replace-it]'",");") FROM information_schema.columns where table_schema = 'your_db';
COLUMN_NAME and TABLE_NAME are columns in the information_schema.columns table, as documented by MySQL
The above query should make the result set:
UPDATE table1 SET field1 = replace(field1,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table1 SET field2 = replace(field2,'[string-to-find]','[string-that-will-replace-it]');
UPDATE table2 SET field3 = replace(field3,'[string-to-find]','[string-that-will-replace-it]');
...
You could output the results of the SELECT statement to a file, which then becomes a script to execute. Alternatively, if you use phpadmin or any other programming language as an interface, you can cycle through the results set, and execute the value of each row in the results set.
I got the idea from MikeW's answer here, about selecting all rows where a data value exists, and from some other stack overflow answers that I have now lost track of, sadly (sorry to the original writers)
To be honest, I think this question may be a duplicate of this, in addition to this though...
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'Text to search, 'Text to replace it with');

Needing to do a simple Find/Replace in SQL

I'm needing to do a very important database string correction on 192 rows and am wondering if this is correct syntax:
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` = REPLACE(`FIELD_NAME`,`REPLACE_THIS_STRING`,`WITH_THIS_STRING`);
Thanks in advance!
The best way to find out is to write it as a SELECT statement first to "preview" the results.
SELECT field_name As before
, Replace(field_name, 'replace this string', 'with this string') As after
FROM table_name
Optional WHERE clause (to only affect the rows that contain our replacement string):
...
WHERE field_name LIKE '%replace this string%'
Well, i would write it like this
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` =
REPLACE(`FIELD_NAME`,`REPLACE_THIS_VALUE`,`WITH_THIS_VALUE`);
Does the table only contain these 192 rows? Otherwise you must add a WHERE to the syntax. Or it will update all FIELD_NAME rows.
As #gvee suggest, try to select the rows first and see how the result looks like to ensure the update scope is correct

Mysql query that replaces a unknown number in a path

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

MySQL Update command

i need to add a special text to all rows in my mysql table ,
how to add some text to the end of all rows' content in a table just for one field
i used this code :
UPDATE `blogs` SET `title`= `title` + 'mytext';
but didnt work for me
UPDATE blogs SET title=concat(title, 'mytext');
MySQL does not have a string concatenation operator (+). You have to use the concat() function, as Daniel Schneller pointed out in the other answer.
If you try SELECT '1' + '2'; in MySQL, it would return 3. The + operator is simply an addition operator. Your query would have updated the title field with a 0.