locked
Convert Bytes to GB RRS feed

  • Question

  • I have two columns in a table with the varchar datatype containing 'TotalSpace' and 'BytesCommitted' - both storage size in bytes. I need it recorded in the database in bytes but reported in GB so my question is what is the SELECT statement to return the results in Gigabytes?

    Thanks in advance
    Adam

     
    Thursday, December 4, 2014 9:14 AM

Answers

  • 1 GB is 1,073,741,824 Bytes, which is 1024 x 1024 x 1024.

    Select bytes /(1024*1024*1024) as GB

    Added: to handle the varchar...

    declare @TotalSpace varchar(100)
    set @TotalSpace = '65039'

    select cast(@TotalSpace as bigint)/(1024.0*1024.0*1024.0) as GB


    • Edited by Kev Riley Thursday, December 4, 2014 9:27 AM
    • Proposed as answer by Russ Loski Thursday, December 4, 2014 9:33 AM
    • Marked as answer by Adam-M Thursday, December 4, 2014 9:35 AM
    Thursday, December 4, 2014 9:17 AM

All replies

  • 1 GB is 1,073,741,824 Bytes, which is 1024 x 1024 x 1024.

    Select bytes /(1024*1024*1024) as GB

    Added: to handle the varchar...

    declare @TotalSpace varchar(100)
    set @TotalSpace = '65039'

    select cast(@TotalSpace as bigint)/(1024.0*1024.0*1024.0) as GB


    • Edited by Kev Riley Thursday, December 4, 2014 9:27 AM
    • Proposed as answer by Russ Loski Thursday, December 4, 2014 9:33 AM
    • Marked as answer by Adam-M Thursday, December 4, 2014 9:35 AM
    Thursday, December 4, 2014 9:17 AM
  • Thanks Kev, I forgot that because it's a varchar I'll have to cast it though. I'll just read up on that and mark it as answer later if it works for me.
    Thursday, December 4, 2014 9:22 AM