21 February 2008

How do I set the default value or binding to a date in the future?

In this article, we will be using SQL Server's Default Value or Binding property to set the default column value to a date in the future. This can be useful if you have a value that you want to expire on a specific date from it's creation.
  1. From SQL Server Management Studio, right-click on the target table and select Modify.
  2. Select the desired column in Object Explorer then move down to the Column Properties section.
  3. In the Default Value or Binding field, enter: (dateadd(year, 30, getdate())) This sets the value to 30 years from the date that the record is entered (unless a value is actually specified during entry).
  4. Save your changes.
  5. You may of course, use different timespans by simply modifying dateadd. For more information, try Google.

No comments: