15

I want to add a nullable boolean column to an existing table with default value of null.

I have used this bit of script but it does not set the default value to null. it sets it to 0 instead.

ADD newColumnName BIT NULL
    CONSTRAINT DF_tableName_newColumnName DEFAULT(null)
4
  • 5
    Why do you need a default constraint on a nullable column, especially when you need the value to be NULL Commented Feb 3, 2016 at 4:36
  • I have 3 states for the value of new column. true, false, null. when the user fills out a form in the application. they can opt to answer to a question with yes or no or they can leave the question unanswered. reaction to No and not answered is not going to be the same. And I need this property of users to be defaulted as null, as if they have not answered to that question. Commented Feb 3, 2016 at 4:44
  • 4
    The default is always NULL - you don't need to explicitly add a default. Commented Feb 3, 2016 at 5:11
  • I believe in SQL the value of a field is NULL by default, no need to explicitly set it to NULL using a DEFAULT constraint. Commented Nov 21, 2019 at 15:32

1 Answer 1

17

I just ran your example code snippet on my SQL Server 2008 R2 instance and then inserted a record. It initialized the column to null, as expected. The next step would be to post the alter statement and the insert statement that you used.

I used:

alter table tmp1 Add newColumnName bit null CONSTRAINT DF_tableName_newColumnName  DEFAULT(null)
insert into tmp1(emp_id) values(9999)
select * from tmp1

After running the above, I used SQL Server Management Studio "Design" action to examine the properties of the new column. It showed that the "Default Value or Binding" was indeed (Null) as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.