Calculating Available Disk Size in SQL Server
Hello everyone,
In this article, I will try to give information about calculating the available disk size in SQL Server.
You can easily do this using the code below.
SELECT Drive,
TotalSpaceGB,
FreeSpaceGB,
PctFree
FROM
(
SELECT DISTINCT
SUBSTRING(dovs.volume_mount_point, 1, 10) AS Drive,
CONVERT(INT, dovs.total_bytes / 1024.0 / 1024.0 / 1024.0) AS TotalSpaceGB,
CONVERT(INT, dovs.available_bytes / 1048576.0) / 1024 AS FreeSpaceGB,
CAST(ROUND(
(CONVERT(FLOAT, dovs.available_bytes / 1048576.0)
/ CONVERT(FLOAT, dovs.total_bytes / 1024.0 / 1024.0) * 100
),
2
) AS NVARCHAR(50)) + '%' AS PctFree
FROM sys.master_files AS mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.file_id) AS dovs
) AS DE;
When you run the above code, you will see a result similar to the one below.
As you can see, the available disk size has been calculated.
Good luck to everyone in business and life.