How to change all bit type column NULL values to false in all database in MSSQL?

Sergey Khojoyan picture Sergey Khojoyan · Sep 6, 2012 · Viewed 30.6k times · Source

I have a database in with I have many bit type columns. After adding other columns I need all old columns to have default "false" values.

Answer

To update each old column to 0, you can use this query (on a column-by-column basis):

UPDATE MyTable
SET OldCol1 = 0
WHERE OldCol1 IS NULL

Then, if you want any future values to have a default of 0 as well, use this:

ALTER TABLE MyTable
ALTER COLUMN OldCol1 bit NOT NULL

ALTER TABLE MyTable
ADD CONSTRAINT OldCol1ShouldBeFalse DEFAULT 0 FOR OldCol1

Now, you'll have to run this against each old column, so hopefully you don't have too many.