[sql-server] How to change the data type of a column without dropping the column with query?

I have a column which has a datatype : datetime. But now i want to convert it to datatype varchar. Can i alter the datatype without droppping the column? If yes, then please explain how?

This question is related to sql-server

The answer is


MSDN says

ALTER TABLE mytable ALTER COLUMN mycolumn newtype

Beware of the limitations of the ALTER COLUMN clause listed in the article


ALTER tablename MODIFY columnName newColumnType

I'm not sure how it will handle the change from datetime to varchar though, so you may need to rename the column, add a new one with the old name and the correct data type (varchar) and then write an update query to populate the new column from the old.

http://www.1keydata.com/sql/sql-alter-table.html


MSDN says

ALTER TABLE mytable ALTER COLUMN mycolumn newtype

Beware of the limitations of the ALTER COLUMN clause listed in the article


it's simple! just type bellow query

alter table table_Name alter column column_name datatype

alter table Message alter column message nvarchar(1024);

it will work happy programming


ALTER TABLE [table_name] ALTER COLUMN [column_name] varchar(150)

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

alter table [table name] remove [present column name] to [new column name.

ALTER TABLE [table_name] ALTER COLUMN [column_name] varchar(150)

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

With SQL server 2008 and more, using this query:

ALTER TABLE [RecipeInventorys] ALTER COLUMN [RecipeName] varchar(550)

ALTER TABLE yourtable MODIFY COLUMN yourcolumn datatype


ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

ALTER tablename MODIFY columnName newColumnType

I'm not sure how it will handle the change from datetime to varchar though, so you may need to rename the column, add a new one with the old name and the correct data type (varchar) and then write an update query to populate the new column from the old.

http://www.1keydata.com/sql/sql-alter-table.html


ALTER TABLE table_name
MODIFY (column_name data_type);

ORACLE - Alter table table_name modify(column_name new_DataType);


ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

MSDN says

ALTER TABLE mytable ALTER COLUMN mycolumn newtype

Beware of the limitations of the ALTER COLUMN clause listed in the article


ALTER tablename MODIFY columnName newColumnType

I'm not sure how it will handle the change from datetime to varchar though, so you may need to rename the column, add a new one with the old name and the correct data type (varchar) and then write an update query to populate the new column from the old.

http://www.1keydata.com/sql/sql-alter-table.html


ALTER tablename MODIFY columnName newColumnType

I'm not sure how it will handle the change from datetime to varchar though, so you may need to rename the column, add a new one with the old name and the correct data type (varchar) and then write an update query to populate the new column from the old.

http://www.1keydata.com/sql/sql-alter-table.html


it's simple! just type bellow query

alter table table_Name alter column column_name datatype

alter table Message alter column message nvarchar(1024);

it will work happy programming


This work for postgresql 9.0.3

 alter table [table name] ALTER COLUMN [column name] TYPE [character varying];

http://www.postgresql.org/docs/8.0/static/sql-altertable.html


ALTER TABLE table_name
MODIFY (column_name data_type);

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20) this is perfect for change to datatype


alter table [table name] remove [present column name] to [new column name.

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

With SQL server 2008 and more, using this query:

ALTER TABLE [RecipeInventorys] ALTER COLUMN [RecipeName] varchar(550)

ALTER TABLE [table name] MODIFY COLUMN [column name] datatype

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20) this is perfect for change to datatype


ALTER TABLE yourtable MODIFY COLUMN yourcolumn datatype


ALTER TABLE [table name] MODIFY COLUMN [column name] datatype

This work for postgresql 9.0.3

 alter table [table name] ALTER COLUMN [column name] TYPE [character varying];

http://www.postgresql.org/docs/8.0/static/sql-altertable.html