Find out how long the sql server service has been running, from t-sql

SELECT crdate FROM sysdatabases WHERE [name] = 'tempdb'

The above will work on SQL Server 2000, 2005 and 2008.

The logic is that the result from the above SQL returns the created date of the tempdb database, which SQL Server recreates every time it is restarted. Hence, the created date of tempdb is the startup time of the server.


SELECT
    login_time
FROM
    sys.dm_exec_sessions
WHERE
    session_id = 1

will give you a datetime for when the server was started.


To get it programmatically, you can run this script. It checks the creation time of your tempdb, since tempdb gets reinitialized every time Sql Server is started.

SELECT create_date 
FROM sys.databases 
WHERE name = 'tempdb'

To make it more intuitive, you can run the script below, which will tell you how many days and hours Sql Server has been running. Minutes and seconds information will be truncated. If you need that, modify the script to get it yourself.

SELECT 'Sql Server Service has been running for about '
+ CAST((DATEDIFF(hh, create_date, GETDATE()))/24 AS varchar(3)) + ' days and '
+ CAST((DATEDIFF(hh, create_date, GETDATE())) % 24 AS varchar(2)) + ' hours'
FROM sys.databases 
WHERE name = 'tempdb'

Source: How long SQL Server has been running