SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Check to ensure YearEnrolled - DateOfBirth is 16
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

fusion86
Starting Member

4 Posts

Posted - 10/05/2013 :  09:25:12  Show Profile  Reply with Quote
Hi all

Appreciate your help all.. I am trying to put in a check to ensure that the YearEnrolled - DateOfBirth is at least 16 years. Please advise me on the correct code.

CREATE TABLE Student(
[DateOfBirth] [date] NOT NULL,
[YearEnrolled] [date] NOT NULL,
SELECT DATEPART(yyyy,DateofBirth) AS BirthYear,
CONSTRAINT DateOfBirth_CHECK CHECK(YearEnrolled-BirthYear =<16),
);

khtan
In (Som, Ni, Yak)

Singapore
17595 Posts

Posted - 10/06/2013 :  03:02:46  Show Profile  Reply with Quote
CREATE TABLE Student
(
	[DateOfBirth] [date] NOT NULL,
	[YearEnrolled] [date] NOT NULL,
	BirthYear as DATEPART(YEAR, [DateOfBirth]),
	CONSTRAINT DateOfBirth_CHECK CHECK (DATEDIFF(YEAR, [DateOfBirth], [YearEnrolled]) >= 16)
);



KH
Time is always against us

Go to Top of Page

fusion86
Starting Member

4 Posts

Posted - 10/09/2013 :  02:10:46  Show Profile  Reply with Quote
Thanks alot for your help!!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000