Whippingboy2004
Technical User
I'm having a problem with my age function returning a decimal point with either one or two trailing zeroes. I've tried using LEFT, but since my ages are being returned as
either single or double digits, it's not been successful.
What I'm trying to accomplish with this is to get the age in months when less than 1 year and age in years otherwise. Each value is followed by 'M' or 'Y' to indicate the units. I'm currently working in SQL 2000.
ALTER FUNCTION [dbo].[FX_GetAgeInYearsOnBirthday] (@Birthdate datetime, @ServiceDate datetime)
RETURNS varchar(4) AS
BEGIN
DECLARE @Age varchar(4)
select @age =
CASE
WHEN ((datepart(year,@Birthdate) = datepart(year,@ServiceDate)))
-- AND (datepart(day,@Birthdate) = datepart(day,@ServiceDate)))
THEN CAST(datediff(month, @Birthdate, @ServiceDate)AS VARCHAR)+'M'
ELSE CAST(LEFT(ROUND(datediff(day,@Birthdate, @ServiceDate)/365.2524,0,1),2)AS VARCHAR)+'Y'
END
Return(@age)
END
Thank you for your help. I really appreciate any input you can offer.
Thanks,
T.
either single or double digits, it's not been successful.
What I'm trying to accomplish with this is to get the age in months when less than 1 year and age in years otherwise. Each value is followed by 'M' or 'Y' to indicate the units. I'm currently working in SQL 2000.
ALTER FUNCTION [dbo].[FX_GetAgeInYearsOnBirthday] (@Birthdate datetime, @ServiceDate datetime)
RETURNS varchar(4) AS
BEGIN
DECLARE @Age varchar(4)
select @age =
CASE
WHEN ((datepart(year,@Birthdate) = datepart(year,@ServiceDate)))
-- AND (datepart(day,@Birthdate) = datepart(day,@ServiceDate)))
THEN CAST(datediff(month, @Birthdate, @ServiceDate)AS VARCHAR)+'M'
ELSE CAST(LEFT(ROUND(datediff(day,@Birthdate, @ServiceDate)/365.2524,0,1),2)AS VARCHAR)+'Y'
END
Return(@age)
END
Thank you for your help. I really appreciate any input you can offer.
Thanks,
T.