Adam Herman
Adam Herman

Reputation: 33

Insert data to a new column, while depending on data from another column

I have added a new column to my table:

Column Name: "Status"
DataType = varchar

In the same table I have an existed column called Bin with datatype INT, and it contains the values: 0 and 1 (1000 rows).

What I want to do, is to insert the word "Success" to "Status" whenever "Bin"=1, and "Failure" when "Bin"=0.

Any ideas how to perform this using SQL statement?

Upvotes: 0

Views: 2020

Answers (2)

Stu
Stu

Reputation: 32599

Instead of adding a column to store completely deterministic data you could add a computed column for this purpose:

alter table T add [Status] as case Bin when 1 then 'Success' else 'Failure' end 

Upvotes: 1

Meyssam Toluie
Meyssam Toluie

Reputation: 1071

Using Case expression you can update your table.

Update table_name
 Set Status = Case Bin 
                 WHEN 1 THEN 'Success'
                 WHEN 0 THEN 'Failure'
               END

Upvotes: 1

Related Questions