Search code examples
sqlsql-servercalculated-columnscreate-table

SQL Server - computed date column


I want to create 'Conferences' table and set EndDate as StartDate + ConferenceDays. Is there any way how to do that?

create table Conferences(
    ConferenceID int not null primary key,
    ConferenceName varchar(50) not null,
    ConferenceDays int not null,
    StartDate date not null,
    EndDate date not null,
)

Solution

  • For SQL server:

    create table Conferences(
        ConferenceID int not null primary key,
        ConferenceName varchar(50) not null,
        ConferenceDays int not null,
        StartDate date not null,
        EndDate AS (dateadd(day,ConferenceDays, StartDate)) PERSISTED
    )