How to use CASE Statements for Conditional Updates In SQL
Case statements within SQL statements provide the ability to execute conditional logic. In this approach, column values are edited whenever the specified condition is met, thus giving a freedom to edit different data columns based on different criteria.
-- We will update the Department Table with the employeeID.
UPDATE EmployeeDetails
SET Department =
CASE
WHEN EmployeeID = 102 THEN ''Finance''
ELSE Department
END;
-- Displaying updated data
SELECT * FROM EmployeeDetails;
Output:
Explanation: With the use of this technique, Department value is updated as “Finance” for the employee with id 102, and the other employee’s department values remain without any changes. That shows how conditional update can be carried out according to the conditions you specify.
How to Set a Column Value to NULL in SQL Server
In the world of database management, SQL Server is a leading and extensively utilized system. A fundamental task within SQL Server is manipulating data within tables, and setting a column value to NULL is a common operation. Whether it’s for maintaining data integrity, performing updates, or meeting specific business logic requirements, efficiently executing this action is crucial.
In this article, we’ll delve into setting column values to NULL in SQL Server. We’ll cover three methods: utilizing UPDATE statements, employing CASE statements for conditional updates, and concatenating values using CONCAT.