Avatar of johnnyg123
johnnyg123
Flag for United States of America asked on

sql query to get age as of a given date

here is some sample data from a table named Customer

CustId   Birthdate
1            05/07/1970
2            05/07/1970
3           05/07/1970


here is some sample data from a table named OrderHistory

CustID    OrderDate        OrderAmount
1              05/07/2019      200
2              05/06/2019      300
3              05/08/2019      400



Trying to write a query that will return the age of the customer as of the order date

in example above, query should return

custid  orderdate           ageasoforderdate

1           05/07/2019       49
2           05/06/2019      48
3           05/08/2019       49
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
Máté Farkas

SELECT oh.CustID, oh.OrderDate, DATEDIFF(year, c.Birthdate, oh.OrderDate) as ageasoforderdate
FROM OrderHistory oh
INNER JOIN Customer c on c.CustID = oh.CustID

Open in new window


Ryan Chong

for your scenario, try use this instead:

Select a.CustID, a.OrderDate,
Floor(DATEDIFF(DAY,b.Birthdate, a.OrderDate) / 365.2425) ageasoforderdate
from OrderHistory a
inner join Customer b on a.CustID = b.CustId

Open in new window

You may need to consider to handle the leap year as well.
arnold

Are we sure the person does not want to know that the person has aged 1 year since the last order?
Your help has saved me hundreds of hours of internet surfing.
fblack61
johnnyg123

ASKER
Hi Ryan

It does appear there does need to be considerations for leap year.  Works fine unless birthday is in february

Ideas?
johnnyg123

ASKER
Hi Arnold,

here is what I have so far

Select a.CustID, a.OrderDate,
case when month(getdate() ) = month(b.BIRTHDATE) and DAY(getdate() ) > =  DAY(b.BIRTHDATE)   then Floor(DATEDIFF(DAY,b.Birthdate, orderdate) / 365.2425) +1  else Floor(DATEDIFF(DAY,b.Birthdate, orderdate) / 365.2425)  end AS AgeOrderDate
from OrderHistory a
inner join Customer b on a.CustID = b.CustId
arnold

Can you actually post what is the output that you are expecting?
Both return the age the person was on the day of last order.

Both Mate's and Ryan's examples will do t
I do not understand why you are using case, what are you testing?

Datediff (year,b.Birthdate,a.orderdate) will return the age 
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ryan Chong

It does appear there does need to be considerations for leap year.  Works fine unless birthday is in february

Ideas?                                  
So, you need to tell us what if customer's DOB is on 29 Feb, how you wish to handle this?
ASKER CERTIFIED SOLUTION
Scott Pletcher

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.