How to Change Column Type in SQL Server

Galaxy Glossary

How do I change a column’s data type in SQL Server without losing data?

ALTER TABLE … ALTER COLUMN modifies a column’s data type, length, or nullability in SQL Server.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why would I change a column type 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.

What is the exact syntax?

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.

How do I change INT to DECIMAL for amounts?

Increase precision to avoid overflow:

ALTER TABLE Orders
ALTER COLUMN total_amount DECIMAL(10,2) NOT NULL;

How do I convert VARCHAR dates to DATETIME?

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;

What if the column is referenced by indexes or constraints?

SQL Server automatically rebuilds indexes. Drop or alter constraints only when the new type conflicts, such as length reductions violating CHECK constraints.

Can I change a column used in FOREIGN KEY?

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;

Best practices for safe type changes

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.

Common mistakes to avoid

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.

Quick reference recap

ALTER TABLE t ALTER COLUMN c NEWTYPE; Works on columns lacking incompatible data, auto-rebuilds indexes, and preserves permissions. Test in staging before production.

Why How to Change Column Type in SQL Server is important

How to Change Column Type in SQL Server Example Usage


--Change primary key type from INT to BIGINT across related tables
BEGIN TRAN;

--Child table first to avoid FK mismatch
ALTER TABLE OrderItems
ALTER COLUMN order_id BIGINT NOT NULL;

--Parent table
ALTER TABLE Orders
ALTER COLUMN id BIGINT NOT NULL;

COMMIT;

How to Change Column Type in SQL Server Syntax


ALTER TABLE table_name
ALTER COLUMN column_name new_data_type [ ( precision [, scale ] ) ] [ NULL | NOT NULL ]
[ WITH ( ONLINE = { ON | OFF } ) ];

--Example in ecommerce context
ALTER TABLE Orders
ALTER COLUMN total_amount DECIMAL(10,2) NOT NULL;

ALTER TABLE Customers
ALTER COLUMN email NVARCHAR(320) NOT NULL;

Common Mistakes

Frequently Asked Questions (FAQs)

Does ALTER COLUMN lock the table?

Standard editions take a schema-modification lock, blocking writes. Enterprise edition with ONLINE=ON performs many changes online.

Can I convert TEXT/NTEXT to VARCHAR(MAX)?

Yes. Use ALTER COLUMN col VARCHAR(MAX) NULL; This is recommended because TEXT/NTEXT are deprecated.

How do I estimate downtime for large tables?

Query sys.dm_exec_requests while testing on a full-size copy. Duration scales with row count, index size, and disk speed.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo