Skip to content
 

Alter Table Alter Column

The Alter Column statement can modify the data type and the Nullable attribute of a column. The syntax is the same for SQL Server 2005 and SQL Server 2008 except 2008 allows the sparse attribute to be changed.

For the example below, we will begin by creating a sample table, then we will modify the columns.

CREATE TABLE dbo.Employee
(
EmployeeID INT IDENTITY (1,1) NOT NULL
,FirstName VARCHAR(50) NULL
,MiddleName VARCHAR(50) NULL
,LastName VARCHAR(50) NULL
,DateHired datetime NOT NULL
)

-- Change the datatype to support 100 characters and make NOT NULL
ALTER TABLE dbo.Employee
ALTER COLUMN FirstName VARCHAR(100) NOT NULL

-- Change datatype and allow NULLs for DateHired
ALTER TABLE dbo.Employee
ALTER COLUMN DateHired SMALLDATETIME NULL

-- Set SPARSE columns for Middle Name (sql server 2008 only)
ALTER TABLE dbo.Employee
ALTER COLUMN MiddleName VARCHAR(100) SPARSE NULL

Columns can be altered in place using alter column statement.

  • Only the datatype, sparse attribute (2008) and the Nullable attribute of a column can be changed.
  • You cannot add a NOT NULL specification if NULL values exist.
  • In order to change or add a default value of a column, you need to use Add/Drop Constraint.
  • In order to rename a column, you must use sp_rename.


Popular search terms:

20 Comments

  1. piyu says:

    i want to modify values of a column of an existing table in sql 2005, can u tell me how to do this?

  2. Mine says:

    Worked well . Thanks

  3. Shahid says:

    Hi, Can we Alter column order with script?

  4. David Henley says:

    I have found myself at this blog many times and each time it answers my question. Thank you, sir!!

  5. TVR says:

    Thanks. saved my time

  6. dan says:

    It’s works on sql server 2008.
    Thank you.

  7. thearit says:

    i want to Edit column in sql language but i don’t know how to do?
    i don’t to Edit one by one COLUMN ?
    i want use only one query to edit all column in my one table .
    please give some solution about this problem !
    thank for your help…!
    KHON SOTHEARIT

  8. Jack Jiang says:

    Good,thank you.

  9. Quiero hacer un alter table a una tabla que contiene una columna number y quiero pasarala a varchar2, pero tiene registros guardados en la tabla.

  10. Sushmitha says:

    Hi,

    I want to alter the datatype of various columns of a table. Is there a way of doing this?

    • Derek Dieter says:

      Hi Sushmitha,

      Yes, this Alter Table will do this. You just need to specify the new data type. Make sure however that all the data values existing in the table will be able to conform to that data type, otherwise you will get an error.

      Good Luck,
      Derek

  11. Sam says:

    Can u tell me how to drop a default value for a column without using the default constraint?

  12. shrinivas says:

    can you give an example of storing images in a table and retrieving them?

    • Salih says:

      More accurately staetd…Move a column after some other columnmysql> ALTER TABLE `mytable` MODIFY COLUMN `mycolumn` [mycolumn definition] AFTER `someothercolumn`;Move a column to the first positionmysql> ALTER TABLE `mytable` MODIFY COLUMN `mycolumn` [mycolumn definition] FIRST;

  13. Julekha says:

    Can you please tell me how can I add constraint to a column after creating the table…

  14. Amy says:

    Can you give me an example of a column, say ‘nvarchar(max)’ with some values already in the column. Without losing the existing values, I want to replace existing values with:
    “prefixText”++”postfixText”

    Is there a way to do this?

    • Derek Dieter says:

      Hi Amy, sure. To do this you need to actually update the column like this:
      UPDATE T
      SET YOURCOLUMN = ‘prepend’ + YOURCOLUMN + ‘postpend’
      FROM yourtable T

      I would try this in a Dec environment first. Also, be aware that this will update all the rows in the table unless yo use a WHERE clause

  15. Ashish says:

    excelent explication!

    Reply to this comment

  16. Leo S. says:

    excelent explication!

post a comment OR Post Your Question on our ASK! Community!