I have a big MySQL table and I want to change the Description column.
I want to delete the beginning of the every cell in each row that matches the value in the Name column, while the rest of the cell remains the same.
Please help.
REPLACE is your friend.
use this to see the changes:
SELECT TRIM(REPLACE(Deescription,`name`,'')) as newField from youtTableName;
update your table
UPDATE youtTableName
SET `Deescription` = TRIM(REPLACE(Deescription,`name`,''))
;
You could use update whete the locate for the name in desciption is 1 (at the begin of the description)
update my_table
set description = substr( description, length(name)+1)
where locate(description, name ) = 1
Related
In a SQL table I have two columns: the first contain a path and the second contains a value.
colunm1
/path/
colunm2
12345
I need to update the first column with the value that exists in the second column. to get this result :
colunm1
/path/12456/
I tried this, but not working
update tablename p
set p.colunm1 = "/path/'colunm2'/"
You have the right idea, but the SQL you shared uses column2 as a string literal. You could use the concat to concatenate the two columns:
UPDATE tablename
SET column1 = CONCAT(column1, column2)
You have to use CONCAT
update tablename p
set p.colunm1 = CONCAT("/path/",`colunm2`,"/");
I have created a table (Table A), which has a column (Column A) which stores values like this
Example:
ASUNMI:GI:PI:INP:EDM:20141001:NO34W:DERERTBYDAY14:NSW
ASUNMI:GI:PI:HME:EDM:20140929:EO23M:WIERTNACAR:VICETC
I need to split this string and place the data in different columns.
Example:
Column2=ASUNMI
Column3=GI
Column4=PI
Column5=INP
Column6=EDM
I need to split the above string based on colons(:).the no of colons in each field could differ hence I cannot use the
substring_index(çolumn,':',-2) property
I need to then use this to update a table
this is a good link please check this out
if you want to update TableA you can write
UPDATE `TableA` SET `columName` = (SELECT SPLIT_STR(columnName, ':',1) as ColumnName from tableName)
or
UPDATE `TableA` SET `columName` = (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(columName,':',1),':',-1) AS columName FROM tableName)
I would like to Populate the username field/column with the first letter of the survey_responders firstname concatenated with the last name of the survey responder.
Every time I run my code it gives me an error :
You can't specify target table 'survey_responders' for update in FROM clause"
Any help with what I'm doing wrong anyone?
update survey_responders
set username = ((select CONCAT(left(first_name,1), last_name)
from survey_responders)
);
try this:
UPDATE survey_responders SET username = CONCAT(LEFT(first_name,1), last_name);
You don't need to do the SELECT query that you did. In UPDATE command, in every row you can use all the columns that you have.
I have two columns in my mysql table, equipment and orderno, Here equipment number is manually inserted, and they are in the form C1234,C3212 etc.
I want to strip the C from equipment column and insert the remaining number to orderno column. I have seen that mysql substring_index() can effectively get substring but I am not sure how to make it automtically do the changes, when the equipment column changes.
You could use two triggers, one that's fired before an INSERT, and one that's fired before an UPDATE, to automatically update orderno based on equipment column:
CREATE TRIGGER upd_your_table BEFORE UPDATE ON your_table
FOR EACH ROW
SET new.orderno=substring(new.equipment, 2)
;
CREATE TRIGGER ins_your_table BEFORE INSERT ON your_table
FOR EACH ROW
SET new.orderno=substring(new.equipment, 2)
;
To update existing values, you could use this:
UPDATE your_table SET orderno=substring(equipment, 2)
See this fiddle
Try this ::
UPDATE myTable set orderno= REPLACE(equipment, 'C', '')
I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;