SQL Server – How to Set Default Value of Column 0 When NULL is Inserted

sql servert-sql

I have a table with a column which has default value to 0. However when null is inserted to this table, I want it to set it to default.

As the insert query is generic and used by other databases too, I cannot make any changes to the insert statement.

Can I have constraints or case statement on create table, so that default 0 value is inserted whenever null is passed.

Best Answer

If the NULL value is explicitly specified, you'll need to use a DML trigger to update NULL values to 0 after insertion. This can't be done with a default constraint.