Over the weekend, I interacted with some of the “wanna be” DBAs. During the interaction I checked with them how they would find when was the SQL Server instance restarted? Everyone had a different approach to find out the information. Here are some of the ways to find out the date & time on which SQL Server service was last restarted.
SQL Server Error Log
By making use of sp_readerrorlog, first time stamp in the SQL Server Error Log can be treated as the time when SQL Server service was restarted.
sp_readerrorlog 0,1,'Copyright (c)'
This approach is not useful if the SQL Server Error Log is cycled manually.
Using sys.dm_os_sys_info DMV
Starting SQL Server 2008, sys.dm_os_sys_info DMV has a column named sqlserver_start_time. This column as the name suggests stores the time when SQL Server was restarted.
SELECT sqlserver_start_time FROM sys.dm_os_sys_info;
But this query does not work in versions which are lower than SQL Server 2008.
Using sys.dm_exec_sessions DMV
The following query will get the Login time of the Session Id 1.
SELECT login_time FROM sys.dm_exec_sessions WHERE session_id = 1;
session_id is created when the SQL Server is started and the login time remains the same until the service is restarted again.
Start time of the Default Trace
The Default Trace is started when the SQL Server is started. The start_time of this trace can also be taken as the time when the SQL Server service was restarted.
select start_time from sys.traces where is_default = 1
This approach is useful except for the instances where the Default Trace is explicitly disabled.
Creating date of tempdb
The creation date of tempdb database will also be the time when SQL Server service was restarted. This is because the tempdb database is re-created whenever the SQL Server service starts up.
SELECT create_date FROM sys.databases WHERE name = 'tempdb'
The above query works on SQL Server 2005 and above. For SQL Server 2000 the same query will vary a little bit.
SELECT crdate FROM sysdatabases WHERE name='tempdb'
This is the query which I always use to find out when the SQL Server service was restarted.