KrzK
KrzK

Reputation: 35

SQL update column 2 and column 3 from value of column 1

I want to write an sql update which, will update 2 columns in every row taking value from other column. For example i have a table

Column 1 |Column 2 | column 3

Value 1 |nothing XX|nothing

VaLUE 2 |nothing XX|nothing

Value 3 | nothing XX|nothing

i want to update column 2 and column 3 from value of column 1. The result table should be like this:

Column 1 |Column 2 | column 3

Value 1 |Value 1|Value 1

VaLUE 2 |VaLUE 2|VaLUE 2

Value 3 | Value 3|Value 3

Any ideas?

Upvotes: 1

Views: 1411

Answers (2)

Charles Bretana
Charles Bretana

Reputation: 146429

update table set
   [Column 2] = [Column 1],
   [Column 3] = [Column 1]

but why? if you just need more than one output column to contain the same value, better is to create computed column

  Alter table [MyTable] add [column2] as column1

this will show in a select * as an additional column, but the data is not persisted twice.

Upvotes: 2

Sean Lange
Sean Lange

Reputation: 33571

Seems completely pointless to make all three columns the same but the code is super simple.

Update SomeTable
set Column2 = column1
, Column3 = Column1

Upvotes: 2

Related Questions