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.

Calculating Available Disk Size in SQL Server

As you can see, the available disk size has been calculated.

Good luck to everyone in business and life.

286 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!