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

I have a column with a data type: date and time. But now I want to convert it to datatype varchar. Can I change the data type without dropping a column? If so, please explain how?

+54
sql-server
Jan 03 '09 at 9:47
source share
15 answers

If ALTER COLUMN is not working.

It is not true that a variable column fails because it cannot do the desired conversion. In this case, the solution is to create a table of dummy tables, TableName_tmp, copy the data using a specialized conversion to the massive Insert command, delete the original table and rename the tmp table to the original table name. You will have to drop and recreate the foreign key constraints, and for performance, you probably want to create the keys after populating the tmp table.

Sounds a lot of work? This is actually not the case.

If you use SQL Server, you can make SQL Server Management Studio for you!

Raise the structure of the tables (right-click on the table and select "Change") and make all your changes (if the column transformation is illegal, just add a new column - you will close it in an instant). Then right-click the background in the Modify window and select Generate Change Script. In the window that appears, you can copy the script change to the clipboard.

Cancel the modification (in the end, you will want to check your script), and then paste the script into a new request window. Modify if necessary (for example, add your conversion by removing the field from the tmp table declaration), and now you have the script needed for the conversion.

+26
Jan 03 '09 at 13:54
source share

MSDN says

ALTER TABLE mytable ALTER COLUMN mycolumn newtype 

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

+87
Jan 03 '09 at 10:55
source share
 ALTER TABLE [table name] MODIFY COLUMN [column name] datatype 
+12
Nov 12 '13 at 11:31
source share
 ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20) 
+10
Jan 03 '09 at 9:55
source share
 ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR(20) 
+7
Jan 03 '09 at 13:42
source share

It's simple! just enter the following query

alter table table_Name alter column column_name datatype

alter table Message alter column message nvarchar(1024);

he will work happy programming

+7
Dec 06 '13 at 12:37
source share

With SQL Server 2008 and more, using this query:

 ALTER TABLE [RecipeInventorys] ALTER COLUMN [RecipeName] varchar(550) 
+6
Jun 22 '12 at 1:18
source share

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

+2
Sep 26 '13 at 7:46
source share
 ALTER TABLE [table_name] ALTER COLUMN [column_name] varchar(150) 
+1
Mar 22 '12 at 3:29
source share

ALTER TABLE YourTableNameHere ALTER COLUMN YourColumnNameHere VARCHAR (20) is ideal for changing the data type

+1
Aug 04 '13 at 7:41
source share

ORACLE - change the table name table_name (column_name new_DataType);

0
Jan 06 '14 at 15:23
source share

ALTER TABLE yourtable MODIFY COLUMN data type yourcolumn

0
Apr 02 '19 at 20:00
source share
 ALTER TABLE table_name MODIFY (column_name data_type); 
-one
Feb 18 '14 at 5:42
source share
 ALTER tablename MODIFY columnName newColumnType 

I'm not sure how it will handle changes from datetime to varchar, 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 request to populate the new column from the old one.

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

-2
Jan 03 '09 at 9:56
source share
 alter table [table name] remove [present column name] to [new column name. 
-four
Aug 15 '13 at 18:57
source share



All Articles