ALTER TABLE … ALTER COLUMN modifies a column’s data type, length, or nullability in SQL Server.
Business logic evolves. You may need more precision for money, store longer text, or migrate an INT flag to a BIT. ALTER TABLE … ALTER COLUMN lets you do this in-place without recreating the table.
Use ALTER TABLE table_name ALTER COLUMN column_name new_data_type [NULL|NOT NULL]. Optional WITH (ONLINE = ON) is available in Enterprise edition for online changes.
Increase precision to avoid overflow:
ALTER TABLE Orders
ALTER COLUMN total_amount DECIMAL(10,2) NOT NULL;
First update bad strings, then alter:
UPDATE Orders
SET order_date = TRY_CONVERT(DATETIME, order_date)
WHERE ISDATE(order_date)=0; -- fix invalid rows
ALTER TABLE Orders
ALTER COLUMN order_date DATETIME NOT NULL;
SQL Server automatically rebuilds indexes. Drop or alter constraints only when the new type conflicts, such as length reductions violating CHECK constraints.
Data types of both sides must match exactly. Alter the child and parent columns in a single transaction:
BEGIN TRAN;
ALTER TABLE OrderItems ALTER COLUMN order_id BIGINT NOT NULL;
ALTER TABLE Orders ALTER COLUMN id BIGINT NOT NULL;
COMMIT;
1️⃣ Backup the database.
2️⃣ Verify convertible data with TRY_CONVERT.
3️⃣ Use BEGIN TRAN and ROLLBACK on error.
4️⃣ Perform in maintenance windows.
5️⃣ Script index rebuilds for large tables.
Implicit truncation—reducing VARCHAR length silently cuts data when ANSI_WARNINGS OFF. Always run a SELECT LEN() check first.
Ignoring NULLability—changing to NOT NULL fails if rows contain NULLs. UPDATE or supply a DEFAULT before the ALTER.
ALTER TABLE t ALTER COLUMN c NEWTYPE; Works on columns lacking incompatible data, auto-rebuilds indexes, and preserves permissions. Test in staging before production.
Standard editions take a schema-modification lock, blocking writes. Enterprise edition with ONLINE=ON performs many changes online.
Yes. Use ALTER COLUMN col VARCHAR(MAX) NULL; This is recommended because TEXT/NTEXT are deprecated.
Query sys.dm_exec_requests while testing on a full-size copy. Duration scales with row count, index size, and disk speed.