Change the data type of a column in a table


To change the data type of a column in a table, use the following syntax:

ALTER TABLE table_name
ALTER COLUMN column_name datatype

In this blog post, we will see how to change the datatypes of the column with an example.

We will create a sample table and change the column datatypes.

USE XYZDb
GO

CREATE TABLE
dbo.SampleTable
(
   
ID INT
   
FirstCol VARCHAR(10), 
   
SecondCol DATETIME
)
GO

ALTER TABLE
dbo.SampleTable
ALTER COLUMN ID VARCHAR(100
)
GO

ALTER TABLE
dbo.SampleTable
ALTER COLUMN FirstCol VARCHAR(100
)
GO

ALTER TABLE
dbo.SampleTable
ALTER COLUMN SecondCol VARCHAR(100
)
GO

That's it.. Your column datatypes are changed...