SQL Server - How to know how long a specific database has been online

Views: 181
Reading Time: 2 minutes

Hey guys!
In this post today, I would like to share a solution I created to answer a question in one of the Telegram groups in which I participate, which was the question of how to know how long a database has been online in SQL Server. That is not how long has the instance been online but how long has a database been online.

To solve this doubt, I thought about using the SQL Server log itself to identify when each database was started and return to the user. The query below will return all banks that are in the last log file, along with the date of the last time the bank was started and the time since then to the current time.

Result:

In case you want to analyze the entire startup history of the databases, scanning through all the log files (not just the last one):

Result:

In the two solutions presented, you can use the “Database” column to filter the selected banks and I believe that the original question was answered by these 2 scripts.

That's it, guys! I hope you liked this tip. Do you know any other solution to answer this question? Leave it here in the comments 🙂

A big hug and until next time!