374

How to change the column size of the salary column in the employee table from numeric(18,0) to numeric(22,5)

Sreedhar Danturthi
  • 7,119
  • 19
  • 68
  • 111

10 Answers10

677
ALTER TABLE [Employee]
ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL
Darren
  • 68,902
  • 24
  • 138
  • 144
  • 69
    It's also important that if the column is with attribute `NOT NULL` then it has to be mentioned in the query else it will set it to `NULL` by default. `ALTER TABLE [Employee] ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL` – Suvendu Shekhar Giri Jun 29 '15 at 07:25
  • 12
    If you have an index on that column you'll have to drop it, then execute the alter table code and then create the index again – Sr.PEDRO Sep 05 '17 at 19:18
53
ALTER TABLE [table_name] ALTER COLUMN [column_name] varchar(150)
Pang
  • 9,564
  • 146
  • 81
  • 122
Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43
46
alter table Employee alter column salary numeric(22,5)
Priyank Patel
  • 6,898
  • 11
  • 58
  • 88
24

Running ALTER COLUMN without mentioning attribute NOT NULL will result in the column being changed to nullable, if it is already not. Therefore, you need to first check if the column is nullable and if not, specify attribute NOT NULL. Alternatively, you can use the following statement which checks the nullability of column beforehand and runs the command with the right attribute.

IF COLUMNPROPERTY(OBJECT_ID('Employee', 'U'), 'Salary', 'AllowsNull')=0
    ALTER TABLE [Employee]
        ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL
ELSE        
    ALTER TABLE [Employee]
        ALTER COLUMN [Salary] NUMERIC(22,5) NULL
Hamid Heydarian
  • 802
  • 9
  • 16
8

Interesting approach could be found here: How To Enlarge Your Columns With No Downtime by spaghettidba

If you try to enlarge this column with a straight “ALTER TABLE” command, you will have to wait for SQLServer to go through all the rows and write the new data type

ALTER TABLE tab_name ALTER COLUMN col_name new_larger_data_type;

To overcome this inconvenience, there is a magic column enlargement pill that your table can take, and it’s called Row Compression. (...) With Row Compression, your fixed size columns can use only the space needed by the smallest data type where the actual data fits.

When table is compressed at ROW level, then ALTER TABLE ALTER COLUMN is metadata only operation.

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
4

Right-click the table you want to modify --> Select "Design" --> Change the value in the "Data Type" column as shown in the following image:

screenshot of the Data Type column with a column's data type value getting changed

Then Save to complete the change to the table design.

GUI method is used for Small tables. You are making change in big tables then use command only and side by side check blocking occur or not till command complete.

Vishe
  • 3,383
  • 1
  • 24
  • 23
  • check properly might be you are missing some step. – Vishe Feb 11 '17 at 08:47
  • 10
    Tools -> Options... / >Designers -> Table and Database Designers -- uncheck "Prevent saving changes that require table re-creation" – 7anner Feb 28 '18 at 01:15
  • 1
    For most things, I prefer scripting. But for column changes, I like the UI. Don't have to remember to check for NOT NULL or any calculations or any additional column properties. Just click Save (after you make the settings change in SSMS as @7anner notes). – Jacob H Dec 31 '18 at 20:03
  • 1
    This is not the correct solution, using Design for changing the Table datatype will result in dropping and recreating the table, you need to use alter ALTER TABLE [Employee] ALTER COLUMN [Salary] NUMERIC(22,5) NOT NULL – Arunkumar Arjunan Aug 16 '21 at 09:43
2

You can use ALTER command to modify the table schema.

The syntax for modifying the column size is

ALTER table table_name modify COLUMN column_name varchar (size);
Dinesh
  • 812
  • 4
  • 14
0

ALTER TABLE "Employee" MODIFY ("Salary" NUMERIC(22,5));

Feng Zhang
  • 1,698
  • 1
  • 17
  • 20
0

For Oracle For Database:

ALTER TABLE table_name MODIFY column_name VARCHAR2(255 CHAR);

Shaini Sinha
  • 527
  • 6
  • 10
-1

In this case, you need to use ALTER TABLE statement to increase column size.

Here is the syntax for it

ALTER TABLE table_name MODIFY column_name varchar(new_length);

Pike
  • 99
  • 1
  • 3