Home > other >  SQL Server : check if user is old enough
SQL Server : check if user is old enough

Time:11-09

I have to check if a user is old enough. I tried as shown here, but this only works for years.

So, for example 11/7/2003 should be true but 12/12/2003 should be false. But, with my code each of these is true.

Here is my code:

[birthdate]date CHECK(DATEDIFF(year,birthdate,getdate()) >= 18),

How can I write this in another way that the months and days will matter?

CodePudding user response:

Want to find people who are at least 18, given then date of birth?

SELECT Cast(CURRENT_TIMESTAMP AS date) AS today
     , DateAdd(yy, -18, Cast(CURRENT_TIMESTAMP AS date)) AS eighteen_years_ago
;

Anyone born on or before eighteen_years_ago is at least 18 years old.

CodePudding user response:

Check the number of days from date A to B and replace the condition with >= 6570 (365*18).

This method does not check for leap years.

  • Related