Skip to content
Advertisement

In SQL how to use if statement using update table command

I am trying to update 2 columns in a table using the update command and I have to use if statement. Table:

For the bill_amount and remarks I need to use if statement, condition:

IF Category code = 1 Bill amount = Units consumed * 0.90, REMARKS = “DOMESTIC”

IF Category code = 2 Bill amount = Units consumed * 2.40, REMARKS = “COMMERCIAL”

IF Category code = 3 Bill amount = Units consumed * 0.50, REMARKS = “FARMERS”

The final output should look like this:Output

Advertisement

Answer

You would use a CASE expression here:

But note that if the underlying data in the table might change, you should instead consider just using a SELECT with the above logic as computed columns. That is, I am suggesting to not do this update if you expect you might ever have to do it again at a later date.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement