Assume a scenario where you have a table that contains columns which are Nullable integer column. You might want to update them to NOT NULL column (adding NOT NULL constraint) and set its value to zero.
How to ALTER a Column from Null to NOT Null in SQL Server ?
This would be a two step process where you should first update the column values to 0 and then write a Data Definition Language (DDL) to alter the column of the table.
Assuming the table Name is “Employee” and the field is LeavesTaken, this is how both the queries would look like.
UPDATE Employee SET LeaveTaken=0 WHERE LeaveTaken IS NULL
The ALTER table query would look like this.
ALTER TABLE Employee ALTER COLUMN LeaveTaken INTEGER NOT NULL
Leave a Reply