Avatar of Charles Baldo
Charles Baldo
Flag for United States of America asked on

Need age at date of document

Hi,

I have two date time fields. One represents the birth date of the person, the other the date of the document.

I need to know at the date of the document how old the person was.

CREATE TABLE [dbo].[tbl_account_documents](
      [account_number] [varchar](12) NULL,
      [doc_date] [datetime] NULL,
      [date_of_birth] [datetime] NULL
) ON [PRIMARY]
Microsoft SQL Server

Avatar of undefined
Last Comment
Charles Baldo

8/22/2022 - Mon
Paul MacDonald

Something like:

SELECT DATEDIFF(YEAR, doc_date, date_of_birth) AS AgeAtDocCreation
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Charles Baldo

ASKER
Thank you
PortletPaul

another method (a case expression decides if you need to deduct 1 or not)

DECLARE @date_of_birth  datetime
SET @date_of_birth ='1937-08-10 00:00:00'

select
     @date_of_birth
   , YEAR(getdate()) - YEAR(@date_of_birth)
    - ( CASE
           WHEN MONTH(@date_of_birth) > MONTH(getdate())
             OR ( MONTH(@date_of_birth) = MONTH(getdate())
                AND DAY(@date_of_birth) > DAY(getdate()) 
                )
           THEN 1
           ELSE 0
         END )  AS Reliable_Age

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Charles Baldo

ASKER
Thank You, a minor variation of Jims did work, appreciate your solution though and will keep it in the "Bag of Tricks"