Home Forums SQL Server 7,2000 T-SQL How can we calculate Age of employee? RE: How can we calculate Age of employee?

  • t.hitendra (5/11/2009)


    Hi,

    you can try this

    select EMP_CODE,DATE_OF_BIRTH ,DATEDIFF(DD,DATE_OF_BIRTH,getdate())/365 as years,(DATEDIFF(DD,DATE_OF_BIRTH,getdate())%365)/30 as months,

    DATEDIFF(DD,DATE_OF_BIRTH,getdate())%365%30 as days

    from EMP_MST where accountid=4

    Hitendra

    Hi,

    create the function like

    CREATE FUNCTION AGE_ASOF_NOW (@EMP_DOB DATETIME,@curr_dt datetime)

    returns varchar(1000)

    AS

    BEGIN

    declare @AGE_ASOF_NOW varchar(100)

    declare @DDIFF int

    select @DDIFF = datediff(DAY,@EMP_DOB,@curr_dt)

    select @AGE_ASOF_NOW = cast((@DDIFF/365)as varchar(4))+' Years '+ cast((@DDIFF%365)/30 as varchar(3))+' Months '+ cast((@DDIFF%365)%30 as varchar(3))+' Days '

    return @AGE_ASOF_NOW

    END

    and call this function in the select statement like

    select Dbo.AGE_ASOF_NOW (DATE_OF_BIRTH,getdate())

    ARUN SAS