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.
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.
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 .
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.
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