Calculating Age
To display the correct age of a person:declare
@date as DATETIME
SET @date = '09/17/1976'
SELECT CAST( datediff(d, @date, getdate()) / 364 as VARCHAR) + ' Years ' +
CAST( datediff(m, dateadd(year, datediff(d, @date, getdate()) / 364, getdate()), getdate()) as VARCHAR) + ' Months ' +
CAST( datepart(d, getdate()) as VARCHAR) + ' Days' AS Age
4 Comments:
Oh yeh! i didnt notice that.
we should also have a check who is entering and increase a few more years if a girl enters the date ;)
I don't think it will give you the right age as in so many years, so many months and so many days
For a little simplicity, I have set a @current date variable instead of using getdate() and replaced the getdate() with @current.
declare
@date as DATETIME,
@current as datetime
SET @date = '08/01/2004'
set @current ='09/02/2005'
SELECT CAST( datediff(d, @date, @current) / 364 as VARCHAR) + ' Years ' +
CAST( datediff(m, dateadd(year, datediff(d, @date, @current) / 364, @current), @current) as VARCHAR) + ' Months ' +
CAST( datepart(d, @current) as VARCHAR) + ' Days' AS Age
Considering @date as the birth date and @current as the current date in the given example I would expect 1 Years -1 Months 1 Days
but it return 1 Years -12 Months 2 Days.
Am I right?
I don't think it will give you the right age as in so many years, so many months and so many days
For a little simplicity, I have set a @current date variable instead of using getdate() and replaced the getdate() with @current.
declare
@date as DATETIME,
@current as datetime
SET @date = '08/01/2004'
set @current ='09/02/2005'
SELECT CAST( datediff(d, @date, @current) / 364 as VARCHAR) + ' Years ' +
CAST( datediff(m, dateadd(year, datediff(d, @date, @current) / 364, @current), @current) as VARCHAR) + ' Months ' +
CAST( datepart(d, @current) as VARCHAR) + ' Days' AS Age
Considering @date as the birth date and @current as the current date in the given example I would expect 1 Years -1 Months 1 Days
but it return 1 Years -12 Months 2 Days.
Am I right?
Its calculating wron age
Post a Comment
<< Home