We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,194 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

SQL Validation Rules for 'Date'

I'm trying to create a table writing in SQL and I need to validate the 'Date of Birth' field

'Players must be over 16 years of age'

So how should I validate this. my table name is : Players
field name is DOB and the datatype is 'DATE'

I tried this 'ALTER TABLE Players SET CHECK DOB >= (16 * 365.25) ;
ERROR "Players must be above 16 years of age."

But it doesn't work. Please help :)

3
Contributors
3
Replies
13 Hours
Discussion Span
2 Months Ago
Last Updated
17
Views
abzy1991
Newbie Poster
9 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I'm trying to create a table writing in SQL and I need to validate the 'Date of Birth' field

Players must be over 16 years of age

You can try this:

ALTER TABLE Players SET CHECK DOB >= DATE + (16 * 365.25);

Not tested.

LastMitch
Industrious Poster
4,177 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45

Why would a person's date of birth be greater than or equal to a number?
either try

DOB >= dateadd(y,-16,getdate()) 

or

datediff(y,DOB,getdate()) >=16
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

Thanks for both of your replies, however none above work.
I've written
1) ALTER TABLE Players SET CHECK
datediff(y,DOB,getdate()) >=16; >>>>>DONT WORK

2) ALTER TABLE Players SET CHECK DOB >= dateadd(y,-16,getdate());
And the error i get is 'missing UNUSED keyword'

I wanted to know, could this be the problem, I've writen the values for Date of Birth as
'14-JUN-1991' >>>Is that why its not working. Instead, should I write it as '14/06/1991'

However if I write it as '14/06/1991' on SQL, that wont work (Just tested it)
and it gives me an error "not a valid month"
NOTE: The DOB field datatype is 'DATE' not 'NUMBER'
Please give your suggestions. Thank you again

abzy1991
Newbie Poster
9 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0636 seconds using 2.67MB