SDU Tools: Last SQL Server Restart

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. A request that I had a while back was to have a function that returned the last time that SQL Server was restarted. So we added the LastSQLServerRestart function.
It takes no parameters.
I used to have a more complex way to work this out, but thanks to a great suggestion from our buddy Rob Wylie, it now just looks for the creation time of tempdb. That’s pretty good and simple to implement.
Find out more
You can see it in action in the main image above, and in the video here. The full current version of the code is also shown below:
You can use our tools as a set or as a great example of how to write functions like these.
Access to SDU Tools is one of the benefits of being an SDU Insider, along with access to our other free tools and eBooks. Please just visit here for more info:
http://sdutools.sqldownunder.com
Latest version of the code
Note: the code might wrap when displayed below.
CREATE FUNCTION SDU_Tools.LastSQLServerRestart()
RETURNS datetime
AS
BEGIN
-- Function: Returns the last time the SQL Server was restarted
-- Parameters: Nil
-- Action: Returns the last time the SQL Server was restarted
-- Return: datetime
-- Notes: Thanks to Rob Wylie for the suggestion
-- Refer to this video: https://youtu.be/bHydP1dzSXE
--
-- Test examples:
/*
SELECT SDU_Tools.LastSQLServerRestart();
*/
RETURN (SELECT create_date FROM sys.databases WHERE [name] = N'tempdb');
END;
2025-02-17