Using a conditional UPDATE statement in SQL

SqlSql Server

Sql Problem Overview


I would like to have an UPDATE statement like this:

 SELECT *
 FROM Employee
 WHERE age = CASE 
 WHEN (age < 20) THEN age=15
 WHEN (age > 20) THEN age= 20

Is this not possible in SQL Server / MySQL? I do not want to use the stored procedures or other things.

Suggest me a suitable way around this problem.

Sql Solutions


Solution 1 - Sql

I think what you want is:

UPDATE EMPLOYEE
SET age =
CASE WHEN AGE < 20 THEN 15
ELSE 20 END

Solution 2 - Sql

You can use a case statement in an update as follows...

UPDATE Employee 
SET Age = CASE WHEN (age < 20) THEN 15
              ELSE 20 END

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaravananView Question on Stackoverflow
Solution 1 - SqlJNKView Answer on Stackoverflow
Solution 2 - SqlEBarrView Answer on Stackoverflow