Get SQL Server Uptime (and SQLAgent Service Status)
SQL Server 2000: Tested
SQL Server 2005: Tested
SQL Server 2008: Tested
SQL Server 2008R2: Tested
SQL Server 2012: Not Tested
Date: 18 Sep 2008
This function doesn't use any DMV or calls to OS functions, but obtains the information from within SQL Server.
TempDB is created from scratch on every restart, so the creation date for this database equates almost exactly to the time that the server was last restarted. It may be a few seconds out, but for all but the most demanding scenarios, this date is acceptable.
The SQL Agent Service status relies on the fact that the service polls msdb on a regular basis and therefore maintains an open (but usually dormant) connection. The code checks for the presence of this connection.
You'll note that the check matches for 2 possible application name strings - this is because the name changed between SQL 2005 and 2008. I have not yet been able to check the SQL 2012 name for the connection