I need to know what function to use to insert a substring into a string of characters in MySql.
This is an example of ua field in my database:
https://www.facebook.com/ExamplePage/
I need to insert the substring pg/ and it needs to look like this when completed:
https://www.facebook.com/pg/ExamplePage/
Note: the first part of all the strings in my fields are the same.
You can do this with a simple replacement:
update `table`
set `field` = replace(field, 'https://www.facebook.com/', 'https://www.facebook.com/pg/')
where `field` like 'https://www.facebook.com/%';
Related
Im trying to update a field only if the field contains certain characters j,k,l,m,n,o,p,q,r
I want to avoid using update tbl set field = field2 where field like '%j' or field like '%k' ...etc.
Is there some syntax to create a list of the characters I am looking for?
Try this in MySQL:
update tbl set field = field2 where field REGEXP 'j|k|l|m|n|o|p|q|r';
You can use regular expressions with the RegExp keywork.
I'm trying to convert a column named A with "YYYY/MM/DD" varchar (already inserted data) to a "YYYY-MM-DD" date. As an example I need the 1987/12/23 (varchar column) data to end up 1987-12-23 (date column), probably using UPDATE and REPLACE.
Try something like this:
UPDATE table
SET A = replace(A, '/', '-')
WHERE A LIKE '____/__/__';
As tadman pointed out, you can use the REPLACE function.
UPDATE `table` SET `A`=REPLACE(`A`,'/','-')
I have some string values inside my Mysql database table. They are in this format:
title1|url1
# or
title1|url1\ntitle2|url2\ntitle3|url3
I want to remove all urls and pipe characters (|) from these values so they should be in this format after the operation:
title1
# or
title1\ntitle2\ntitle3
I tried this query so far (say my table is table1 and the field is values):
UPDATE `table1` SET `values` = SUBSTRING_INDEX(`values`, '|', 1);
For values like title1|url1 it works but for values like title1|url1\ntitle2|url2 it returns title1.
How should I do that?
[UPDATE]
url is any url-like string, such as http://www.example.com/ or www.example.org or http://example.com or anything else.
UPDATE `table1` SET `values` = REPLACE (`values`, '|url', '');
I am trying to remove name_ part of each name in database, name_ is mistakenly inserted into db and now in 30 object names. if i remove manuelly from db, it takes me much time.
one example is: name_john. it should be john.
how can i delete this name_ from all names of all objects in db with some sql statement?
If it are column values you can do it with an update statement.
UPDATE table_reference
SET column_reference = SUBSTRING(column_reference, 6)
WHERE column_reference LIKE 'name\_%' ESCAPE '\'
If this is about column values that you need to modify, you could use the REPLACE() function like this:
UPDATE tablename
SET columnname = REPLACE(columnname, 'name_', '')
WHERE columnname LIKE '%name\_%' ESCAPE '\'
;
That would remove all entries of name_ in columnname. If there can be no more than one entry (or if only one needs to be removed) and its position is fixed, you could use the INSERT() function instead, which, despite its name, can also replace and delete substrings. This is how you could use it if the position of name_ was e.g. at the beginning:
UPDATE tablename
SET columnname = INSERT(columnname, 1, 5, '')
WHERE columnname LIKE 'name\_%' ESCAPE '\'
;
I have a database table in MYSQL with around 1000 rows. In the table I have a column called 'overview'. In each row, this column has some value and at the end of that value I have a specific line (text) starting with: 'Source...'
Now what I want is, I want to remove this line from each column and replace it with some other text content.
I believe it can be accomplished with some smart query.
You can simply use REPLACE in your query like this
UPDATE your_table SET col_name = REPLACE(col_name , ‘Source...’, ‘new_val’)
WHERE col_name LIKE '%Source...';
Check Out the SQLFIDDLE.
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string).
The syntax of REPLACE is:
REPLACE (text_string, from_string, to_string)
In your case, you can do this way:
UPDATE `tableName` SET `column` = REPLACE(column , 'Source...', 'Replaced Value')
Use Replace
update TBL
set overview = Replace(picture, 'Source..', 'replacement')
keep a backup of the table before anything.Or you can do it on a copy.
you can do this by following:
update table_name set col_name = replace(column_name , ‘Source...’, ‘Replaced String...’);