Tuesday, October 18, 2022

.::: Query Check SQL Server Uptime MSSQL :::.

 1. Check

SELECT @@servername as servername,getdate() as TimeCollect,sqlserver_start_time, DATEDIFF(Day, sqlserver_start_time,getdate()) as sql_uptime_day ,
DATEDIFF(hour, sqlserver_start_time,getdate()) as sql_uptime_hours,DATEDIFF(minute, sqlserver_start_time,getdate()) as sql_uptime_minutes,
DATEDIFF(second, sqlserver_start_time,getdate()) as sql_uptime_second,DATEDIFF(millisecond, sqlserver_start_time,getdate()) as sql_uptime_milisecond
FROM sys.dm_os_sys_info;


 2.  Query 2


DECLARE @start_time datetime;
DECLARE @uptime_seconds int;

-- Mendapatkan waktu awal saat layanan SQL Server mulai
SELECT @start_time = create_date FROM sys.databases WHERE name = 'tempdb';

-- Mendapatkan waktu sekarang
DECLARE @current_time datetime = GETDATE();

-- Menghitung jumlah detik uptime
SELECT @uptime_seconds = DATEDIFF(SECOND, @start_time, @current_time);

-- Konversi detik ke hari, jam, menit, detik
DECLARE @days int, @hours int, @minutes int, @seconds int;

SET @days = @uptime_seconds / (24 * 3600);
SET @hours = (@uptime_seconds % (24 * 3600)) / 3600;
SET @minutes = (@uptime_seconds % 3600) / 60;
SET @seconds = @uptime_seconds % 60;

-- Output
SELECT @@servername as ServerName,@days AS 'Days', @hours AS 'Hours', @minutes AS 'Minutes', @seconds AS 'Seconds',@@version as VersionSQL;

 


3. query 3

 
DECLARE @start_time datetime;
DECLARE @uptime_seconds int;

-- Mendapatkan waktu awal saat layanan SQL Server mulai
SELECT @start_time = create_date FROM sys.databases WHERE name = 'tempdb';

-- Mendapatkan waktu sekarang
DECLARE @current_time datetime = GETDATE();

-- Menghitung jumlah detik uptime
SELECT @uptime_seconds = DATEDIFF(SECOND, @start_time, @current_time);

-- Konversi detik ke hari, jam, menit, detik
DECLARE @days int, @hours int, @minutes int, @seconds int;

SET @days = @uptime_seconds / (24 * 3600);
SET @uptime_seconds = @uptime_seconds % (24 * 3600);

SET @hours = @uptime_seconds / 3600;
SET @uptime_seconds = @uptime_seconds % 3600;

SET @minutes = @uptime_seconds / 60;
SET @seconds = @uptime_seconds % 60;

-- Output dalam format hari:jam:menit:detik
SELECT @@servername as ServerName,
    CAST(@days AS varchar(10)) + ':' +
    RIGHT('0' + CAST(@hours AS varchar(2)), 2) + ':' +
    RIGHT('0' + CAST(@minutes AS varchar(2)), 2) + ':' +
    RIGHT('0' + CAST(@seconds AS varchar(2)), 2) AS 'Uptime',@@version as VersionSQL;

 



No comments:

Post a Comment

Popular Posts