tags:

views:

15

answers:

1

I am new to this site, so sorry if I sound like a newb. I have found some good answers to stuff on this site, and I like it!

Here is my current dilemma:

I am storing a bigint value (for file sizes) in a table. I need to group on one column and for the filesizes (which are in bytes) I would like to have a column showing them by GB. This would mean Sum(FileSize/1024/1024/1024) which is not showing any decimal places. My research seems to indicate this may be due to trucation rather than rounding.

I have tried many options of casting and converting, but cannot seem to find any information about how to sum and divide a bigint, and maintain the decimals. If I take the sum and divide it in Excel, I get the decimals, which tells me there has to be a way to do this in SQL.

Any help is appreciated.

+3  A: 
Sum(CAST(FileSize AS FLOAT)/1024/1024/1024)
Gabriel McAdams
Thanks! I also found that this would work: case when cast(sum(d.filesize)/1024.0/1024.0/1024.0 as decimal(10,5))is nullthen '0' else cast(sum(d.filesize)/1024.0/1024.0/1024.0 as decimal(10,5)) end as SessionSizeGB I will try yours to see the difference.
misssqlfun
Don't forget to accept this answer if you found it useful.
Gabriel McAdams
I'm sorry. I don't know how. I am so new to this site. I clicked the checkmark. Is there something else?
misssqlfun