|
|
Question : SQL: Calculating the difference between two dates - working out the age of user given DOB
|
|
Hello This question is regarding MS SQL db. I have the following table:
Area | Limit (int) ------------------------ Area1 | 18 Area2 | 21 Area3 | 0 Area4 | 18 Area5 | 54
In SQL, how do I work out the difference between two dates?
I am trying to write a stored procedure, where I pass a parameter @DOB, which is the users' date of birth. Then I need to work out how old the user is and compare it to the area which the are trying to access and see if they are old enough to enter. [By returning true or false or some other kind of indicator]
Also, for example, If they are trying to access Area3, since its limit is 0, we assume then by default that Limit is 21.
Any help would be greatly appreciated. Thanks.
|
Answer : SQL: Calculating the difference between two dates - working out the age of user given DOB
|
|
as gbshahaq mentionnes, you say MS SQL, but posted in MySQL zone?
if it is MS SQL Server: SELECT * FROM areas WHERE @DOB <= DATEADD(year, - CASE WHEN limit = 0 THEN 21 ELSE limit END, getdate())
|
|
|
|
|