54

I'm trying to overwrite values that are found in TYPE1 with values that are found in TYPE2.

I wrote this SQL to try it out, but for some reason it isn't updating:

select * from stuff

update stuff
set TYPE1 = TYPE2
where TYPE1 is null;

update stuff
set TYPE1 = TYPE2
where TYPE1 ='Blank';

http://www.sqlfiddle.com/#!3/a4733/17

Any reason why my values in TYPE1 are not updating?

10 Answers 10

75

This works for me

select * from stuff

update stuff
set TYPE1 = TYPE2
where TYPE1 is null;

update stuff
set TYPE1 = TYPE2
where TYPE1 ='Blank';

select * from stuff
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, you're right. But after running the update statements once, I would run the select statement again... but no change. Is there a new instance of a fiddle after every run?
@Keven Yes, each SQLFiddle execution is independent of the previous run.
59
UPDATE a
SET a.column1 = b.column2
FROM myTable a 
INNER JOIN myTable b
on a.myID = b.myID

in order for both "a" and "b" to work, both aliases must be defined

5 Comments

the accepted answer works, but this is is a bit more direct and to the point
I subtracted a point because this doesn't actually answer my original question. I wanted to update a value from TYPE1 with the value from TYPE2 IN THE SAME TABLE.
Actually the example above answers the question because the JOIN is done on the same table as the FROM statement, if you look carefully "myTable" is referenced twice. It's just a little more verbose than the accepted answer.
@mastazi Thanks for pointing that out! I completely glossed over the fact that it was the same table.
This is the best answer for sure. This is very useful when you're taking data in a column and manipulating it before putting in another column. For example I'm taking my PART_NUMBER column and removing the dashes from it and placing it in MOD_PART_NUMBER. Best way to do this!
37
UPDATE TABLE_NAME SET COLUMN_A = COLUMN_B;

Much easier. At least on Oracle SQL, i don't know if this works on other dialects as well.

5 Comments

Yeah, works for SQL Server as well (so your answer is perfectly valid for this question).
I got error: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
This should be the answer. Worked for me. With huge concat of 5 columns into one field.
worked in postgres. Also it easy to combine with another query like UPDATE table_name SET column_1 = cast(split_part(column_2, ' ', 1) as float);
simple and works
3

You put select query before update queries, so you just see initial data. Put select * from stuff; to the end of list.

Comments

3

This answer about updating column from a part of another column in the same table.

update T1
set domainname = (New value) --Example: (SELECT LEFT(TableName.col, CHARINDEX('@',TableName.col)-1) STRIPPED_STRING FROM TableName where TableName.col = T2.Emp_ID)
from TableName T1
INNER JOIN
    TableName T2
ON 
    T1.ID= T2.ID;

1 Comment

the safest solution
3
update TABLE_1 a set COLUMN_1 = (select COLUMN_2 from TABLE_1 b where a.ID = b.ID)

1 Comment

SET or WITH expected, got 'a'
1

Your select statement was before the update statement see Updated fiddle

Comments

0
UPDATE `tbl_user` SET `name`=concat('tbl_user.first_name','tbl_user.last_name') WHERE student_roll>965

Comments

0

If you need to make an operation on a specific column:

update courses set hours = days * 5

Comments

0

You can simply do this .

Update TableName SET Column1=Column2 where id=id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.