Find out how long the sql server service has been running, from t-sql

后端 未结 6 1385
春和景丽
春和景丽 2021-02-07 22:11

I\'d like if its possible to work out from inside sql server how long sql server has been running.

Would like to use this in conjunction with one of the DMV\'s for unuse

6条回答
  •  离开以前
    2021-02-07 22:46

    To get it programmatically, you can run this script. It checks the creation time of your tempdb, since tempdb gets reinitialized every time Sql Server is started.

    SELECT create_date 
    FROM sys.databases 
    WHERE name = 'tempdb'
    

    To make it more intuitive, you can run the script below, which will tell you how many days and hours Sql Server has been running. Minutes and seconds information will be truncated. If you need that, modify the script to get it yourself.

    SELECT 'Sql Server Service has been running for about '
    + CAST((DATEDIFF(hh, create_date, GETDATE()))/24 AS varchar(3)) + ' days and '
    + CAST((DATEDIFF(hh, create_date, GETDATE())) % 24 AS varchar(2)) + ' hours'
    FROM sys.databases 
    WHERE name = 'tempdb'
    

    Source: How long SQL Server has been running

提交回复
热议问题