SQL Server Check Constraint on Date of Birth column to check if person is age 18 or over

kt06eoxx  于 2023-03-22  发布在  其他
关注(0)|答案(2)|浏览(142)

New to SQL Server and need help with check constraint.

How can I create a check constraint on DateOfBirth column to only allow date of birth which is age 18 or greater? Example of current syntax:

Create Table Member (
    Memberid Int IDENTITY (1,1) PRIMARY KEY,
    Title Varchar(10),
    Firstname Varchar(40),
    Lastname Varchar(40),
    DateOfBirth Date Not Null
);
j2qf4p5b

j2qf4p5b1#

Try this one mate!

create table person (DateOfBirth date check (DATEDIFF(year,DateOfBirth,getdate()) > 18))
lf5gs5x2

lf5gs5x22#

Try this one Mate, It'll allow those who are 18 or above only

create table person (DateOfBirth date 
            Check(DATEADD(year,-18,GETDATE()) >= DateOfBirth ))

相关问题