93

I came across an old table today with a datetime column called 'Created' which allows nulls. Now, I'd want to change this so that it is NOT NULL, and also include a constraint to add in a default value (getdate()).

So far I've got the following script, which works fine provided that I've cleaned up all the nulls beforehand:

ALTER TABLE dbo.MyTable ALTER COLUMN Created DATETIME NOT NULL 

Is there any way to also specify the default value as well on the ALTER statement?

2
  • do you want to fill all the already existing records with today's date? Commented Jul 6, 2010 at 13:52
  • Yep; that would suffice. Commented Jul 6, 2010 at 13:56

5 Answers 5

117

I think you will need to do this as three separate statements. I've been looking around and everything I've seen seems to suggest you can do it if you are adding a column, but not if you are altering one.

ALTER TABLE dbo.MyTable
ADD CONSTRAINT my_Con DEFAULT GETDATE() for created

UPDATE MyTable SET Created = GetDate() where Created IS NULL

ALTER TABLE dbo.MyTable 
ALTER COLUMN Created DATETIME NOT NULL 
Sign up to request clarification or add additional context in comments.

2 Comments

Has anyone else actually tried this? I've found that the second statement fails due to null value in the table (despite the presence of a default value). You need to insert a step between those two where you clean out the existing nulls.
Looks like someone edited the answer. It is now correct and complete.
13

You may have to first update all the records that are null to the default value then use the alter table statement.

Update dbo.TableName
Set
Created="01/01/2000"
where Created is NULL

Comments

12

you need to execute two queries:

One - to add the default value to the column required

ALTER TABLE 'Table_Name` ADD DEFAULT 'value' FOR 'Column_Name'

i want add default value to Column IsDeleted as below:

Example: ALTER TABLE [dbo].[Employees] ADD Default 0 for IsDeleted

Two - to alter the column value nullable to not null

ALTER TABLE 'table_name' ALTER COLUMN 'column_name' 'data_type' NOT NULL

i want to make the column IsDeleted as not null

ALTER TABLE [dbo].[Employees] Alter Column IsDeleted BIT NOT NULL

1 Comment

Does not appear to work with SQL Server: the null values already in the table are not affected.
3

If it's SQL Server, you can do it on the column properties within design view.

Try this:

ALTER TABLE dbo.TableName 
  ADD CONSTRAINT DF_TableName_ColumnName
    DEFAULT '01/01/2000' FOR ColumnName

4 Comments

This needs to be in script form so that it can be run as part of our automated release process.
Still no dice. The constraint gets added; but it still barfs when it tries to convert the NULL to NOT NULL
Script to update all the nulls to set the value to your default then apply it? Sorry, ain't being much help!
I eliminated the statement to convert null to not null and the solution works. Since there is a default value defined, all my new entries are getting my set default so I appear to have no risk for null values. Just make sure your code never sets a null value in your update statements.
1

Try this

ALTER TABLE table_name ALTER COLUMN col_name data_type NOT NULL;

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.