Search code examples
sqlsql-serverdatabaseinsertmultiple-columns

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?


Solution

  • Using Case expression you can update your table.

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