By default sql server assigns boolean
fields a NULL
value. How can I tell it to use '0'
as default? I tried setting the default value to ((0)) but it still persists on the NULL.
Bookmark this question. Show activity on this post. By default sql server assigns boolean fields a NULL value.
Multiple bit datatypes in a table are collected into bytes. For example, 7 bit columns fit into 1 byte; 9 bit columns take 2 bytes. Columns with a datatype of bit cannot be NULL and cannot have indexes on them.
To insert a new value to the BIT column, use INSERT statement: INSERT INTO table_name (bit_column) VALUES (1); You can also use TRUE and FALSE as the inputs for the BIT columns, SQL Server will automatically convert them as follow: TRUE will be converted to 1.
Here's a sample with a non nullable bit column with the default specified, just run the below in Management Studio:
CREATE TABLE #temp ( id INT , myBit BIT NOT NULL DEFAULT 0 -- not null with default of false ); INSERT INTO #temp ( id ) -- only insert to id col, the default will set itself VALUES ( 123 ); INSERT INTO #temp ( id, myBit ) VALUES ( 456, 1 ) -- this insert adds a true value to override the default SELECT * FROM #temp; DROP TABLE #temp;
Produces:
id myBit 123 0 456 1
ALTER TABLE [table name] ADD [column name] BIT NOT NULL DEFAULT 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With