• This code gives the start time for the OS, SQL Server, and SQL Server Agent, and gives the uptime days for the OS, SQL Server, and SQL Server Agent.

    select

    [OS Start Time]= convert(varchar(23),b.OS_Start,121),

    [SQL Server Start Time]= convert(varchar(23),a.SQL_Start,121),

    [SQL Agent Start Time]= convert(varchar(23),a.Agent_Start,121),

    [OS Uptime] =

    convert(varchar(15),

    right(10000000+datediff(dd,0,getdate()-b.OS_Start),4)+' '+

    convert(varchar(20),getdate()-b.OS_Start,108)),

    [SQL Uptime] =

    convert(varchar(15),

    right(10000000+datediff(dd,0,getdate()-a.SQL_Start),4)+' '+

    convert(varchar(20),getdate()-a.SQL_Start,108)) ,

    [Agent Uptime] =

    convert(varchar(15),

    right(10000000+datediff(dd,0,getdate()-a.Agent_Start),4)+' '+

    convert(varchar(20),getdate()-a.Agent_Start,108))

    from

    (

    Select

    SQL_Start = min(aa.login_time),

    Agent_Start =

    nullif(min(case when aa.program_name like 'SQLAgent %' then aa.login_time else '99990101' end),

    convert(datetime,'99990101'))

    from

    master.dbo.sysprocesses aa

    where

    aa.login_time > '20000101'

    ) a

    cross join

    (

    select

    OS_Start = dateadd(ss,bb.[ms_ticks]/-1000,getdate())

    from

    sys.[dm_os_sys_info] bb

    ) b

    Results:

    OS Start Time SQL Server Start Time SQL Agent Start Time OS Uptime SQL Uptime Agent Uptime

    ----------------------- ----------------------- ----------------------- --------------- --------------- ---------------

    2012-08-04 22:19:26.317 2012-08-04 23:38:07.163 2012-10-31 11:49:49.063 0100 18:28:38 0100 17:09:57 0013 04:58:15