Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 370
Sergey Khojoyan Avatar asked Sep 06 '12 13:09

Sergey Khojoyan


People also ask

How do I change all NULL values in SQL?

ISNULL Function in SQL Server To use this function, all you need to do is pass the column name in the first parameter and in the second parameter pass the value with which you want to replace the null value. So, now all the null values are replaced with No Name in the Name column.

How do I change a column from NULL to NOT NULL in SQL Server?

All you need to do is to replace [Table] with the name of your table, [Col] with the name of your column and TYPE with the datatype of the column. Execute the command and you are allowed to use NULL values for the specified column. That is all it takes to switch between NULL and NOT NULL .


1 Answers

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.

like image 199
LittleBobbyTables - Au Revoir Avatar answered Oct 07 '22 00:10

LittleBobbyTables - Au Revoir