Swap the Values of Two Columns in SQL Server
Estimated study time: 2 minutes. A quick, practical fix you'll use more often than you'd expect.
Sometimes you need to exchange the values stored in two columns — for example, swapping first_name and last_name after a data entry mistake. SQL Server gives you a few clean ways to do this.
Method 1: Single UPDATE Statement
SQL Server evaluates the SET clause using the original row values, so you can swap columns directly in one statement — no temporary variable needed:
UPDATE employees
SET first_name = last_name,
last_name = first_name;
Method 2: Using a Temporary Variable (Row by Row)
DECLARE @temp VARCHAR(100); SELECT @temp = first_name FROM employees WHERE employee_id = 101; UPDATE employees SET first_name = last_name WHERE employee_id = 101; UPDATE employees SET last_name = @temp WHERE employee_id = 101;
Method 3: Using CASE for Conditional Swaps
UPDATE products
SET price = CASE WHEN category = 'Sale' THEN cost ELSE price END,
cost = CASE WHEN category = 'Sale' THEN price ELSE cost END;
When You'd Use This
- Fixing data entry errors where two fields were reversed
- Reassigning values during a data migration
- Quick fixes during QA or testing on a staging database
Why the One-Line Swap Works in SQL Server
SQL Server treats all assignments in a single UPDATE as happening at the same moment. Every expression on the right side of SET reads the row as it was before the statement started, so first_name = last_name does not affect what last_name = first_name sees. This all-at-once behaviour follows the SQL standard and is also how PostgreSQL and Oracle work.
The Same Statement Behaves Differently in MySQL
Because this page sits with MySQL material, this point deserves attention. MySQL evaluates single-table UPDATE assignments from left to right, using values already changed earlier in the same statement. Running the SQL Server one-liner in MySQL sets both columns to the old last name, and the original first name is lost.
A safe MySQL approach uses a temporary column, relying on that left-to-right order:
-- MySQL
ALTER TABLE employees ADD COLUMN tmp_name VARCHAR(100);
UPDATE employees
SET tmp_name = first_name,
first_name = last_name,
last_name = tmp_name
WHERE employee_id IN (101, 102);
ALTER TABLE employees DROP COLUMN tmp_name;
Always test on a copy of the table first, because the result depends entirely on the engine's evaluation rules.
A Safer Workflow for Any Swap
A swap touches two columns at once, so mistakes are harder to spot afterwards. This sequence keeps you in control in SQL Server.
-- 1. Preview old and new values side by side
SELECT employee_id,
first_name AS old_first, last_name AS old_last,
last_name AS new_first, first_name AS new_last
FROM employees
WHERE employee_id IN (101, 102);
-- 2. Run the swap inside a transaction
BEGIN TRANSACTION;
UPDATE employees
SET first_name = last_name,
last_name = first_name
WHERE employee_id IN (101, 102);
SELECT @@ROWCOUNT AS rows_changed;
SELECT employee_id, first_name, last_name
FROM employees
WHERE employee_id IN (101, 102);
-- 3. Keep or undo
COMMIT TRANSACTION;
-- ROLLBACK TRANSACTION;
If the row count is not what the preview suggested, roll back and check the WHERE clause before trying again.
Things That Can Break a Swap
- Different column sizes. Moving a 100-character value into a VARCHAR(50) column fails with a truncation error, and the whole statement stops.
- Different data types. Swapping a date column with a text column forces implicit conversion, which may fail or silently change formats.
- Constraints. A NOT NULL column cannot receive a NULL from the other side, and unique constraints may be violated during the swap.
- Triggers and audit tables. UPDATE triggers fire for every affected row, which can be slow on large tables and fill audit logs.
- Computed columns. These cannot be targets of an UPDATE at all.
When renaming beats updating
If the data is correct but the columns were simply labelled the wrong way round across the whole table, renaming is faster than rewriting every row:
EXEC sp_rename 'employees.first_name', 'tmp_col', 'COLUMN';
EXEC sp_rename 'employees.last_name', 'first_name', 'COLUMN';
EXEC sp_rename 'employees.tmp_col', 'last_name', 'COLUMN';
Renaming can break views, stored procedures and application code that use the old names, so search for dependencies before you do it.