Get Instance UpTime Using SQL Server

This article describes how to determine instance UpTime using SQL Server.
  • 1830

In this article I describe how to determine instance uptime. Follow given steps.

Step 1

Logon to SSMS and connect to database engine.

Step 2

Open New Query window.

Step 3

Write following code in New Query window.

declare @startupDate datetime

declare @days bigint

declare @hours bigint

declare @minutes bigint

declare @seconds bigint

select  @startupDate = CrDate,

@seconds = DateDiff(second, CrDate, getdate()),

@days = @seconds/60/60/24,

@seconds = @seconds - (@days*60*60*24),

@hours = @seconds/60/60,

@seconds = @seconds - (@hours*60*60),

@minutes = @seconds/60,

@seconds = @seconds - (@minutes*60)

from    sysdatabases (nolock)

where   [name] = 'TempDb'

select  @startupDate startup_time,cast(@days as varchar) + ' days ' +

case when @hours < 10 then '0' else '' end + cast(@hours as varchar) + ':' +

case when @minutes < 10 then '0' else '' end + cast(@minutes as varchar) + ':' +

case when @seconds < 10 then '0' else '' end + cast(@seconds as varchar)

as online_duration

Output:

uptime.jpg

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.