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 '\'
;
Related
I have a database on MySQL where the data in the "Name" column contains unwanted carets (^). For example, a name might be "John^ Test^^". Is there any way to remove all these characters from the column?
May be this can help.
Update
Yourtable
Set
Name = replace(name, '^', '');
You could do something like this:
UPDATE table SET column = REPLACE(myColumn, '^', ' ')
I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.
You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.
You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.
If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?
I have a database table, with a column that contains integers. Each entry is a phone number, and they are all missing a zero at the beginning.
e.g. I have 798514586,
785558999
I want to run a SQL query that will modify each entry by putting a zero infront of it.
So the result will be
0798514586,
0785558999
IS there such a query to do this?
Try this
Syntax:
UPDATE <table> SET <column_to_update> = CONCAT(<string_to_concat>, <column_to_update>)
Example:
UPDATE contacts SET phone = CONCAT('0', phone)
I suppose you dont't want to add leading zero if it already exists:
update TableName
set SomeColumn = concat('0', SomeColumn)
where SomeColumn not like '0%'
It's not a good idea to store phone numbers as INTs, it's better to use a VARCHAR here. I would suggest you do add a new varchar column:
ALTER TABLE numbers ADD COLUMN phone_new VARCHAR(15);
then you can use an UPDATE query:
UPDATE numbers
SET
phone_new = CONCAT('0', phone)
MySQL will automatically cast the number to a string, and using CONCAT you can add a leading zero.
You can try by this:
update tableName set fieldName = CONCAT('0',fieldName)
You can use LPAD :
Update _table set _col = LPAD(_col , 10, '0');
Say, you got a table of 100 records. And field age contains a some integers. And you want all those integers to be incremented by 1.
or you got a textfield called name and a bunch of names in there. And you want all those names to be prefixed as Mr..
Is there a way to achieve this in one SQL command?
The alternative would be to compile a recordset of these 100 recs and going thru a loop and then running an individual update statement.
Use the update command
update yourtable
set age=age +1
update yourtable
set name = 'Mr. ' + name
where gender='M'
UPDATE mytable SET age = age+1
UPDATE mytable SET name = CONCAT('Mr. ', name)
If MySQL is in ANSI mode – specifically, PIPES_AS_CONCAT, you can use 'Mr. ' || name instead.
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...’);