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

Time:03-07

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?

CodePudding user response:

Using Case expression you can update your table.

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

CodePudding user response:

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 
  • Related