I have an attachments table that stores the size of the document in Bytes.
I'm needing to display a result set of all documents in either KB or MB. In KB if the document is less than 1MB or in MB if the document is larger than 1MB.
At the moment I have this within my query, but I'm struggling to get the display properly formatted in terms of rounding.
CASE WHEN D.DocumentSize < 1000000 THEN
CONCAT(D.DocumentSize / 1024, 'KB')
ELSE
CONCAT(D.DocumentSize / 1048576, 'MB')
END AS DocumentSizeText,
Here are some example values:
87336
1458250
346
8434
8434
346 bytes is going to display 0KB also, so ideally displaying 1KB would be ideal as a minimum.
4条答案
按热度按时间pftdvrlh1#
Use
FORMAT
if you want to have a specified significant number of decimal values in MB.N3
here is for showing 3 decimal numbers.hgc7kmma2#
How do you want to round the values? If you want to round up, use
ceiling()
:ghhaqwfi3#
In the original question, it suggested the values should be rounded up when it said
346 bytes is going to display 0KB also, so ideally displaying 1KB would be ideal as a minimum
However clarification comments against an answer suggest that decimal values should be shown, rather than rounding:
It displays 2MB though for 1458250 when it should be 1.45MB
Further, the question said
I'm needing to display a result set ... in KB if the document is less than 1MB or in MB if the document is larger than 1MB
However the suggested answer posed in the question looked at splitting the data on 1000000 bytes, which is really only an approximation of 1 megabyte and all prior answers followed that lead. Funnily enough, when formatting the data, the exact value of a megabyte is used: 1048576.
I'll provide three answers below -
I'll extend the answer to show Gigabytes too (even though the question doesn't ask for it - just remove that clause if it is not required, but I think readers may find this extended definition helpful). In both cases I'll use the accurate definitions of megabyte and gigabyte. Full credit goes to the earlier answers which actually solved the initial problem.
Rounding up to nearest value
If dealing with file size, this is being conservative and counting more bytes than is actually being reported.
Showing 3 decimal values
Perhaps more accurate, and less readable.
Rounding properly (up or down)
More accurate than rounding up ... when summing multiple values.
tez616oj4#