Question

I'm trying to add a constraint to a table so that it displays one of the columns as the current date plus 10 days. Here's what I've tried so far (I'm very new to SQL):

ALTER TABLE         orders
ADD CONSTRAINT  default_date
DEFAULT         DATEADD (DAY,10,required_date) FOR required_date

Halp!

Edit: I've also tried this now:

ALTER TABLE         orders
ALTER COLUMN        required_date
ADD CONSTRAINT      required_date_plus_ten
DEFAULT             DATEADD (DAY,10,required_date)

Edit: Thanks to ypercube & my classmate. The final code is:

ALTER TABLE       orders
ADD CONSTRAINT    default_date
DEFAULT           (DATEADD (DAY,10,'required_date')) FOR required_date;
Was it helpful?

Solution

The syntax in SQL-Server, for adding a DEFAULT value to an existing column is:

ALTER TABLE     orders
ADD CONSTRAINT  required_date_plus_ten
DEFAULT         DATEADD(day, 10, GETDATE())
FOR             required_date ;

Tested in SQL-Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top