To set different values for different rows in one UPDATE, put a CASE expression in the SET clause. SQL Server tests each row against the WHEN conditions in order. The first condition that is true decides the new value. If none is true, the ELSE value is used, or NULL when there is no ELSE.
UPDATE table_name
SET column_name = CASE
WHEN condition_1 THEN value_1
WHEN condition_2 THEN value_2
ELSE value_3
END
WHERE filter_condition;
The WHERE clause is optional, but it decides which rows change. Without it, every row in the table is updated.
Why use CASE in an UPDATE
You could run one UPDATE per condition, but that can give wrong results when the conditions overlap. Say one statement gives every score from 80 to 89 a grade of B, and a second statement changes every B with a score under 85 to C. The second statement also changes the rows the first one just set. A single UPDATE with CASE avoids this, because each row takes the result of only its first matching WHEN.
Sample table
The examples use a Students table in the dbo schema:
CREATE TABLE dbo.Students (
StudentID INT PRIMARY KEY,
StudentName NVARCHAR(50) NOT NULL,
Score INT NULL,
Grade NVARCHAR(2) NULL,
Status NVARCHAR(10) NULL
);
INSERT INTO dbo.Students (StudentID, StudentName, Score)
VALUES (1, N'Ana', 95),
(2, N'Ben', 84),
(3, N'Chen', 71),
(4, N'Dara', 58),
(5, N'Eli', NULL);
Set a value for each score range
UPDATE dbo.Students
SET Grade = CASE
WHEN Score >= 90 THEN 'A'
WHEN Score >= 80 THEN 'B'
WHEN Score >= 70 THEN 'C'
ELSE 'F'
END
WHERE Score IS NOT NULL;
After this runs, the table holds:
| StudentName | Score | Grade |
|---|---|---|
| Ana | 95 | A |
| Ben | 84 | B |
| Chen | 71 | C |
| Dara | 58 | F |
| Eli | NULL | NULL |
Ana’s score of 95 matches the first WHEN, so the later conditions are never checked for her. The order of the branches matters. If Score >= 70 came first, Ana, Ben and Chen would all get C, because each of their scores is at least 70.
Eli has no score, so the WHERE clause skips him and his Grade stays NULL.
Keep existing values with ELSE
A CASE with no ELSE gives NULL to every row that matches no WHEN, and that overwrites any value already there. Suppose Grade already holds values and you run this:
UPDATE dbo.Students
SET Grade = CASE
WHEN Score >= 90 THEN 'A'
WHEN Score >= 80 THEN 'B'
END;
Chen (71) and Dara (58) now have NULL in Grade, even if they had a grade before. To leave them alone, return the column’s current value in the ELSE:
UPDATE dbo.Students
SET Grade = CASE
WHEN Score >= 90 THEN 'A'
WHEN Score >= 80 THEN 'B'
ELSE Grade
END;
If you would rather have a default such as 0 instead of NULL for rows that match nothing, put that value in the ELSE. For numbers, the same idea is covered in how to get 0 instead of NULL in SQL. Filling blank columns is a related job, and how to insert values in empty column in sql covers that case.
NULL values and comparisons
A comparison with NULL is neither true nor false, so it never matches a WHEN. With the usual ANSI_NULLS setting, WHEN Score = NULL is never true. Test for missing values with IS NULL instead:
CASE
WHEN Score IS NULL THEN 'No score yet'
WHEN Score >= 70 THEN 'Passed'
ELSE 'Failed'
END
A NULL score also fails every comparison, so it falls through to the ELSE. In the first example, leaving out the WHERE clause would have given Eli an F. That is why the WHERE Score IS NOT NULL filter is there.
Update several columns in one statement
Each column in the SET list can have its own CASE:
UPDATE dbo.Students
SET Grade = CASE
WHEN Score >= 90 THEN 'A'
WHEN Score >= 80 THEN 'B'
WHEN Score >= 70 THEN 'C'
ELSE 'F'
END,
Status = CASE
WHEN Score >= 70 THEN 'Passed'
ELSE 'Failed'
END
WHERE Score IS NOT NULL;
Within one UPDATE, every expression reads the row as it was before the statement ran. So the Status CASE above tests Score, not the new Grade. If Status should depend on the new grade, run a second UPDATE after the first.
Check the rows before you change them
Three steps help you catch mistakes before they matter.
First, run the same CASE in a SELECT so you can see the new values without changing anything:
SELECT StudentID,
StudentName,
Score,
CASE
WHEN Score >= 90 THEN 'A'
WHEN Score >= 80 THEN 'B'
WHEN Score >= 70 THEN 'C'
ELSE 'F'
END AS NewGrade
FROM dbo.Students
WHERE Score IS NOT NULL;
Second, wrap the update in a transaction. Start with BEGIN TRANSACTION, check the table with a SELECT, and then run COMMIT TRANSACTION to keep the changes or ROLLBACK TRANSACTION to undo them.
Third, use the OUTPUT clause to return the old and new values from the update itself. In OUTPUT, deleted holds the values before the change and inserted holds the values after it:
UPDATE dbo.Students
SET Grade = CASE
WHEN Score >= 90 THEN 'A'
WHEN Score >= 80 THEN 'B'
WHEN Score >= 70 THEN 'C'
ELSE 'F'
END
OUTPUT deleted.StudentName,
deleted.Grade AS OldGrade,
inserted.Grade AS NewGrade
WHERE Score IS NOT NULL;
Use values from another table
If the rule depends on another table, join that table in the FROM clause of the UPDATE. If you are new to joins, how to write an inner join query in SQL is a good starting point.
Be careful with this form. If more than one row from the joined table matches a row being updated, the result of the update is undefined. Make sure the join matches at most one source row for each row you update.
Data types and errors
- Each
THENandELSEvalue must convert to the data type of the column. Writing'A'into anINTcolumn fails with a conversion error. - If every
THENandELSEresult is the literalNULL, SQL Server returns error 8133. Make at least one branch return a column or a typed value. - Keep the conditions simple enough to read. A long
CASEis easier to check when you test it in aSELECTfirst.
One UPDATE or several
A CASE in one UPDATE gives each row exactly one result. It is usually easier to read than several statements run one after another. It is not always the fastest option on very large tables, so test it on your own data.
Checklist
- Preview the new values with a
SELECTthat uses the sameCASE. - Add a
WHEREclause to limit the rows. Without one, every row changes. - Use
ELSEwith the column itself to keep current values, or give an explicit default. - Use
IS NULL, not= NULL. - Make sure every
THENvalue fits the column’s data type. - Put the most specific conditions first.