top of page

Query how many days has the server been running without a reboot?

Method #1

Select dateDiff(day,login_time, GetDate()) as 'Days Running'
From sys.dm_exec_sessions
WHERE login_time = (SELECT MIN(login_time) FROM sys.dm_exec_sessions)
 

This code uses the sys.dm_exec_sessions system view to retrieve the login_time of the session with the earliest login_time value. It then calculates the number of days between that login_time and the current date and time using the DATEDIFF function. The result is returned as the number of Days Running.

This code will give you the number of days he SQL Server instance has been running without a reboot.


Method #2

SELECT DATEDIFF(day, create_date, GETDATE()) AS 'Days Running'FROM sys.databases
WHERE name = 'tempdb'

This code uses the sys.databases system view to retrieve the create_date of the tempdb database. It then calculates the number of days between that create_date and the current date and time using the DATEDIFF function. The result is returned as the number of Days Running.

This code will give you the number of days that the SQL Server instance has been running without a reboot, based on the create_date of the tempdb database. The tempdb database is re-created every time the SQL Server instance is restarted, so the create_date of the tempdb database can be used as an indicator of when the SQL Server instance was last restarted.


This information is also available in my SQL Server M&M product as a daily report (Shown Below)






Recent Posts

See All

Commenti

Valutazione 0 stelle su 5.
Non ci sono ancora valutazioni

Aggiungi una valutazione

Get in Touch

Thanks for submitting!

bottom of page