Calculating disk space

  • Good day experts,

    I am going through some sql queries on my envronment.There is this query below which calculates disk space.I just want to know that why do we have to use /1024 three times.

    Select left(Volumedescription,1) as driveName,

    cast(cast(Volumesize as numeric(20,2))/1024/1024/1024 as numeric(13,2)) as TotalSpace,

    cast(cast(VolumeSpaceAvailable as numeric(20,2))/1024/1024/1024 as numeric(13,2)) as FreeSpace.

    I am new to sql.

    Thanks in advance

  • Hi,

    I don't know the source of your query, but if you got bytes, you had to divide 3 time

    Bytes / 1024 = KB

    KB / 1014 = MB

    MB / 1024 = GB

    Regards,

    Andreas

  • Thank you .Now i know

  • Piling on, here is an easy way to remember this

    😎

    DECLARE @X_BYTES BIGINT = 10240000000000;

    SELECT

    @X_BYTES / POWER(CONVERT(BIGINT,2,0),10) AS KILO_BITES

    ,@X_BYTES / POWER(CONVERT(BIGINT,2,0),20) AS MEGA_BITES

    ,@X_BYTES / POWER(CONVERT(BIGINT,2,0),30) AS GIGA_BITES

    ,@X_BYTES / POWER(CONVERT(BIGINT,2,0),40) AS TERA_BITES

    ;

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply