How to change the data type of a column without dropping the column with query?

Sql Server

Sql Server Problem Overview


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?

Sql Server Solutions


Solution 1 - Sql Server

MSDN says

ALTER TABLE mytable ALTER COLUMN mycolumn newtype

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

Solution 2 - Sql Server

If ALTER COLUMN doesn't work.

It is not unusual for alter column to fail because it cannot make the transformation you desire. In this case, the solution is to create a dummy table TableName_tmp, copy the data over with your specialized transformation in the bulk Insert command, drop the original table, and rename the tmp table to the original table's name. You'll have to drop and recreate the Foreign key constraints and, for performance, you'll probably want to create keys after filling the tmp table.

Sound like a lot of work? Actually, it isn't.

If you are using SQL Server, you can make the SQL Server Management Studio do the work for you!

  • Bring up your table structure (right-click on the table column and select "Modify")
  • Make all of your changes (if the column transformation is illegal, just add your new column - you'll patch it up in a moment).
  • Right-click on the background of the Modify window and select "Generate Change Script." In the window that appears, you can copy the change script to the clipboard.
  • Cancel the Modify (you'll want to test your script, after all) and then paste the script into a new query window.
  • Modify as necessary (e.g. add your transformation while removing the field from the tmp table declaration) and you now have the script necessary to make your transformation.

Solution 3 - Sql Server

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

Solution 4 - Sql Server

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

Solution 5 - Sql Server

Type the below query:

alter table table_Name alter column column_name datatype

e.g.

alter table Message alter column message nvarchar(1024);

Solution 6 - Sql Server

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20)

Solution 7 - Sql Server

With SQL server 2008 and more, using this query:

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

Solution 8 - Sql Server

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

Solution 9 - Sql Server

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

Solution 10 - Sql Server

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

Solution 11 - Sql Server

ORACLE - Alter table table_name modify(column_name new_DataType);

Solution 12 - Sql Server

ALTER TABLE yourtable MODIFY COLUMN yourcolumn datatype

Solution 13 - Sql Server

ALTER TABLE table_name
MODIFY (column_name data_type);

Solution 14 - Sql Server

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

Solution 15 - Sql Server

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamikshaView Question on Stackoverflow
Solution 1 - Sql ServerdevioView Answer on Stackoverflow
Solution 2 - Sql ServerMark BrittinghamView Answer on Stackoverflow
Solution 3 - Sql ServerPratyayView Answer on Stackoverflow
Solution 4 - Sql ServercapotejView Answer on Stackoverflow
Solution 5 - Sql ServerSiwachGauravView Answer on Stackoverflow
Solution 6 - Sql ServerLeon TaysonView Answer on Stackoverflow
Solution 7 - Sql Serverduc14sView Answer on Stackoverflow
Solution 8 - Sql ServeratrichkovView Answer on Stackoverflow
Solution 9 - Sql ServerSyed Baqar HassanView Answer on Stackoverflow
Solution 10 - Sql Serveruser2650031View Answer on Stackoverflow
Solution 11 - Sql ServerVindaView Answer on Stackoverflow
Solution 12 - Sql ServerMeder MamutovView Answer on Stackoverflow
Solution 13 - Sql Serverfaisal khanView Answer on Stackoverflow
Solution 14 - Sql ServerJesse PepperView Answer on Stackoverflow
Solution 15 - Sql ServerATUL KARMAKARView Answer on Stackoverflow