User970008
User970008

Reputation: 1155

Update SQL: Take Value from One Column and Replace Null in Another

I have a table that has old, legacy data along with new data. The old data does not have all the fields populated like the new data. I want to update the old data so that it matches the new stuff. The fields are ints and represent user IDs. The Created_By field should default to the User_ID -- meaning that in the legacy data, we're setting the created_by value to be the same as the user_id.

Example fields:

Created_By | User_ID
NULL       | 1234
NULL       | 2345
NULL       | 1234
NULL       | 6742
1234       | 1234
2345       | 1234

So I want to only update the NULL values. However, I do not know how to grab the User_ID for each row.

Something like this:

update my_table
set Created_By = ??? (the respective User_ID for that row)
where Created_By is null

So the update should look like this:

Created_By | User_ID
1234       | 1234
2345       | 2345
1234       | 1234
6742       | 6742
1234       | 1234
2345       | 1234

Upvotes: 0

Views: 2447

Answers (1)

juergen d
juergen d

Reputation: 204766

update my_table
set Created_By = User_ID
where Created_By is null

Upvotes: 8

Related Questions