Reputation: 4408
Background: I wish to convert bytes to MB using SQL Server.
This is what I am doing:
(CONVERT([int], ROUND([Free space bytes] / ((1024) * (1024)), (0))))
I would like to know if this looks correct, as it is returning results, however, I wish to see if this is the most efficient way to do this.
Upvotes: 1
Views: 2721
Reputation: 222622
Your expression computes the MB by dividing the input value by 1024 * 1024
: this part is correct, although you have unneeded parentheses:
[Free space bytes] / 1024 / 1024
Assuming that the column is of int
datatype, this gives you an integer value: the decimal part is just truncated, so there is no need to round()
and convert()
to int
.
On the other hand if the column is decimal, or if you want to actually round the results to the closest integer, then:
convert(int, round([Free space bytes] / 1024.0 / 1024, 0))
The .0
at the end of the first divisor forces decimal context, if the column is int
to start with.
Upvotes: 3