Moons
Moons

Reputation: 3854

Sql Server Constraint based on two dates difference

I have three columns two are of date types and one is of int

The third column stores the difference between two dates in years.

What could be the constraint for this?

The colums are like:

total_years   int           
from_year     datetime  
to_year       datetime  

The total_years is the difference between two dates(in years)

Upvotes: 1

Views: 310

Answers (1)

Martin Smith
Martin Smith

Reputation: 453358

Seems like you need a computed column not a constraint

CREATE TABLE YourTable
  (
     from_year DATETIME,
     to_year   DATETIME,
     total_years AS DATEDIFF(YEAR, from_year, to_year)
  )  

Upvotes: 3

Related Questions