I am trying to remove not null constraint in sql server 2008 without losing data.
Asked
Active
Viewed 2e+01k times
4 Answers
259
ALTER TABLE YourTable ALTER COLUMN YourColumn columnType NULL

Omu
- 69,856
- 92
- 277
- 407

Michael Pakhantsov
- 24,855
- 6
- 60
- 59
-
5I found I had to include the type in YourColumn eg. ALTER TABLE YourTable ALTER COLUMN YourColumn int NULL – Adam Butler Jun 28 '11 at 01:16
-
or you can do : alter table table_name modify column_name type(30) NULL. 30 being the size of your column type, example: varchar(30) – nr5 Sep 19 '12 at 18:11
-
69In postgres: `ALTER TABLE YourTable ALTER COLUMN YourColumn DROP NOT NULL` – Shane Mar 20 '13 at 16:41
3
Remove constraint not null
to null
ALTER TABLE 'test' CHANGE COLUMN 'testColumn' 'testColumn' datatype NULL;
-
-
3@HopeKing the question was about Microsoft SQL Server and not MySql. – Orchidoris Feb 18 '19 at 14:00
-
I'm finding that our copy of MS SQL Server 2019 isn't familiar with the `change` keyword. – 15ee8f99-57ff-4f92-890c-b56153 Jan 13 '23 at 19:09
2
Remove column constraint: not null
to null
ALTER TABLE test ALTER COLUMN column_01 DROP NOT NULL;

PasQualE
- 37
- 4
-
3That doesn't look like valid T-SQL. Although the question was marked with SQL, note that the question explicitly refers to SQL Server which only accepts T-SQL. – TT. Apr 18 '19 at 14:06