ChatGPT解决这个技术问题 Extra ChatGPT

How do you change the datatype of a column in SQL Server?

I am trying to change a column from a varchar(50) to a nvarchar(200). What is the SQL command to alter this table?


s
siddhartha jain
ALTER TABLE TableName 
ALTER COLUMN ColumnName NVARCHAR(200) [NULL | NOT NULL]

EDIT As noted NULL/NOT NULL should have been specified, see Rob's answer as well.


Why should it be specified? What is the benefit? Everything I want to leave as it is I don't have to mention, I think.
@TheincredibleJan That would be cool, but unfortunately it doesn't work that way. If you issue an ALTER TABLE TableName ALTER COLUMN ColumnName command to change an existing column that is [NOT NULL], and you don't explicitly specify it, it will be [NULL] afterwards, as that's the default.
This only works if you don't have constraints and indexes on that column, otherwise, you need to drop everything and recreate it, which is tedious and grunt work, especially if you have many foreign references on that column. e.g. when changing from tinyint to int.
Careful if you have an IDE open (like SSMS). Even with my Designer Tab closed on the Table I was Altering (after running the script and right-clicking on the Table to select "Design") it still showed the old Data Types! It was only after closing ALL of my Tabs in Management Studio and opening the Design View again that it finally showed the updated DataType. Very scary, so be careful (it may be a cache-bug that is fixed by now or one MS never bothers fixing). For those wondering why I ran T-SQL when I use Design-View, I wanted to alter my DataTypes to SysName (which SSMS does not allow).
I had no luck with these commands in XAMPP.ALTER TABLE table MODIFY COLUMN column datatype worked for me.
R
Rob Garrison

Don't forget nullability.

ALTER TABLE <schemaName>.<tableName>
ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]

What is the matter with nullability? If I don't want to change it - what is the benefit in setting it again?
^ It will default to NULL after the alter table statement finishes executing without explicitly defining what it should be.
@sc305495 To be exact, it will default to whetever your ANSI_NULL_DEFAULT settings is.
J
John Sansom

Use the Alter table statement.

Alter table TableName Alter Column ColumnName nvarchar(100)

p
pbaris

The syntax to modify a column in an existing table in SQL Server (Transact-SQL) is:

ALTER TABLE table_name
    ALTER COLUMN column_name column_type;

For example:

ALTER TABLE employees
    ALTER COLUMN last_name VARCHAR(75) NOT NULL;

This SQL Server ALTER TABLE example will modify the column called last_name to be a data type of VARCHAR(75) and force the column to not allow null values.

see here


please add more information to your answer and format the code / query!
A
Alexander Zaldostanov

For changing data type

alter table table_name 
alter column column_name datatype [NULL|NOT NULL]

For changing Primary key

ALTER TABLE table_name  
ADD CONSTRAINT PK_MyTable PRIMARY KEY (column_name)

j
jocassid

As long as you're increasing the size of your varchar you're OK. As per the Alter Table reference:

Reducing the precision or scale of a column may cause data truncation.


M
Muhammad Omair
ALTER TABLE [dbo].[TableName]
ALTER COLUMN ColumnName VARCHAR(Max) NULL

This doesn't seem to add anything over the existing accepted answer from years ago, and also the changes you've posted to the table structure don't actually match the question.
B
Biman Pal

in 11g:

ALTER TABLE TableName
Modify ColumnName DataType;

EG: ALTER TABLE employees Modify BIRTH_DATE VARCHAR(30);


m
mxmissile
ALTER TABLE [Performance].[dbo].[CRA_283_Violation]
ALTER COLUMN [Throughput_HS_DC_NodeB_3G_Alarm] bit

M
Marius

Try this:

ALTER TABLE "table_name"
MODIFY "column_name" "New Data Type";

"Modify" obviously is not correct. Please compare with other answers.
This is old, but appears that Kai Tzer was proving MySql and/or Oracle DDLs.
Agree with Sheldon, the answers here not helped me with oracle sql, just this