One of the main goals with this blog is to write stuff when we learn / discover new things or smarter and more efficient ways to do things – and that is spot on what I’ll do today.
Finding the time when SQL Server was last restarted, and using that timestamp to calculate the actual runtime is something that we all do – some of us many times a day – personally I use this number (runtime) when I look at some of the SQL Server statistics.
And this is where i learned some thing new this week. In the past to get the timestamp when SQL Server was started I used to use a variant of this query:
SELECT start_time FROM sys.dm_exec_requests WHERE (session_id = 1) SELECT login_time FROM sys.sysprocesses WHERE (spid = 1) SELECT MIN(login_time) FROM sys.sysprocesses
TempDb is created every time SQL Server is started, so another workaround is to look at the creation date on the TempDb database. Something like this:
SELECT create_date FROM sys.databases WHERE database_id = 2 -- (TempDb)
So far this is what I used to do, and now to the new thing that I learned this week . With SQL Server 2008 the DMV sys.dm_os_sys_info returns a column called sqlserver_start_time. This column is Datetime and contains the date and time SQL Server was last started.
So from now on to find the SQL Server start time, I’ll be using this query:
SELECT sqlserver_start_time FROM sys.dm_os_sys_info