SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculate Age


Calculate Age

Author
Message
Maysarah
Maysarah
SSC-Addicted
SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)

Group: General Forum Members
Points: 491 Visits: 145
Comments posted to this topic are about the item Calculate Age
Robert Seliga
Robert Seliga
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 48
why not just datediff() the number of days between the parameter and getdate() - then divide that number by 365.25?
prvangala
prvangala
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 12
Dividing the datediff in days by 365.25 doesn't always work (consistently). Sometimes you have to delete the fraction part (floor) and sometimes you have to round up to the next integer (ceiling) to get the correct age. Here are two examples (assume the first date as DOB, and the second as the day the age is calculated on):
select datediff(day, '10/23/2006', '5/23/2008')/365.25, datediff(day, '5/23/1899', '5/23/2008')/365.25
returns
1.582477 108.999315

To get the right age in integer value, we have to floor the first value(1.582477), and ceiling the second value (108.999315).
Given that, I'd alter the original function by adding the second date as a parameter as well. Then you can pass getdate() or any other date as the second param.

-Prabhakar
Mark Ingalls
Mark Ingalls
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 32
How 'bout this?


declare @dob as datetime
select @dob = '6/21/1968'
declare @now as datetime
declare @year_diff as tinyint
select @now = getdate()
select @year_diff = datediff(year, @dob, @now)
if DateAdd(year, @year_diff, @dob) > @now
select @year_diff -1
else
select @year_diff





basically, calculate the number of years that have gone by, then add those years back to the date of birth. if the resulting date is in the future, we subtract one from the years as it would indicate that we have not yet reached the birthday in the current year. if the resulting date is in the past, we have already reached the birthday in this year, so we can return the number of years unaltered.

thanks,
mark
Dmitriy Burtsev
Dmitriy Burtsev
SSC Veteran
SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)

Group: General Forum Members
Points: 209 Visits: 445
Thank you for idea

create function dbo.Calc_Age (@dob datetime) -- date of birth
returns int
as
begin
return ( select datediff (year, @dob,v_date) -
case
when (month(@dob) = month(v_date) and day(@dob) > day(v_date)) then 1
when month(@dob) > month(v_date) then 1
else 0
end
from dbo.v_getdate )
end
Maysarah
Maysarah
SSC-Addicted
SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)SSC-Addicted (491 reputation)

Group: General Forum Members
Points: 491 Visits: 145
Mark, your Idea is very nice Smile
wasekm2001
wasekm2001
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
Hello, Mark Ingalls,

This is Wasek, I am just a beginer in this business, but I am eager to get the solution of calculating a person's age and date difference from one date to another for my project. Could you pl make a sample project with those in VB 2008, Zip it and send it across to my e-mail address below I willbe pleased.

wasekm2001@yahoo.com

One more thing, if you know. Which I could not do. That, how can I change the page setup of a print form component. Meaning to say that its paper size and the margins are auto set. How can I set it my self of my desire. If you know give me easy solution. Again I need a sample project of yours in VB 2008, considering my capability as a learner. Thanks a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search