Finding someone's age in SQL

前端 未结 7 2278
孤街浪徒
孤街浪徒 2020-12-16 21:11

In a SQL Server database, I record people\'s date of birth. Is there an straight-forward method of working out the person\'s age on a given date using SQL only?

Usi

相关标签:
7条回答
  • 2020-12-16 22:12

    Check out this article: How to calculate age of a person using SQL codes

    Here is the code from the article:

    DECLARE @BirthDate DATETIME
    DECLARE @CurrentDate DATETIME
    
    SELECT @CurrentDate = '20070210', @BirthDate = '19790519'
    
    SELECT DATEDIFF(YY, @BirthDate, @CurrentDate) - CASE WHEN( (MONTH(@BirthDate)*100 + DAY(@BirthDate)) > (MONTH(@CurrentDate)*100 + DAY(@CurrentDate)) ) THEN 1 ELSE 0 END 
    
    0 讨论(0)
提交回复
热议问题