Converting bytes to GB with exact decimals in SQL Server [duplicate] - sql

This question already has answers here:
Decimal values in SQL for dividing results
(6 answers)
Closed 3 years ago.
The first 2 columns have data in bytes. I am trying to convert it from Bytes to GB. For some reason its not reflecting properly in Free Space Column
select vmi.VMTotalMaxSize, vmi.VMTotalSize,
CONVERT(decimal(10,2),vmi.VMTotalMaxSize/1024/1024/1024) as [VMTotalMaxSize (GB)],
CONVERT(decimal(10,2),vmi.VMTotalSize/1024/1024/1024) as [VMTotalSize (GB)],
CONVERT(decimal(10,2),(vmi.VMTotalMaxSize-vmi.VMTotalSize)/1024/1024/1024) as [VMFreeSpace (GB)]
from tbl_WLC_VMInstance vmi
VM Total Max Size:375809638400, 268435456000, 214748364800
VMTotalSize: 375683809280, 62755176448, 74662805504
VMTotalMaxSize (GB): 350.00, 250.00, 200.00
VMTotalSize (GB):349.00, 58.00, 69.00
VMFreeSpace (GB)0.00, 191.00, 130.00
It would nice to have the data with proper calculation. Thanks

Divide by 1024.0, not 1024 - otherwise, your result is converted to integer thus losing decimal precision.

Related

SQL syntax - division and ceiling functions [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 2 years ago.
I am trying to calculate how many packs of sweats a kid needs based on the amount of individual sweets required. I am trying to figure out the SQL syntax to do this.
I have used ceiling with divide but still not working
select CEILING(NoOfSweets / SweetsPerPack) AS NoOfPacks
Scenarios:
NoOfSweets SweetsPerPack RequiredOutCome NoOfPacks
--------------------------------------------------------
10 10 1 1
5 10 1 0
20 10 2 2
8 10 1 0
7 5 2 1
If the values are integers, then SQL Server does integer division. So, 1/2 is 0 rather than 0.5. I find that the simplest way to get a number with decimal points is to multiply by 1.0:
CEILING(NoOfSweets * 1.0 / SweetsPerPack)

Convert String into digit in SQL [duplicate]

This question already has answers here:
Converting words to numbers in PHP
(6 answers)
Closed 8 years ago.
How to convert any string into digit in SQL without CASE and Decode function.
eg. THREE to 3
FOUR to 4
FIVE to 5
SIX to 6
Range is not decided.. can be vary upto N.
Well, I'm not sure whether this is what you need, but what about defining a table, say digits, like this:
digit: text | value: int
------------+-----------
one | 1
two | 2
three | 3
etc.
Then use a query, for example, like this one:
SELECT value FROM digits WHERE digit = 'FIVE'
Sure, it's pretty weird (to say the least), but nonetheless the use of CASE is avoided.

Configure float with decimal in sql or force string to show decimal [duplicate]

This question already has answers here:
Is there a way to cast float as a decimal without rounding and preserving its precision?
(3 answers)
Closed 9 years ago.
I have a table in SQl with a column in float type , this table is used to send values to a one fiscal printer,
code name price
----------------------------------
34 cUP 2,5
36 BOOK 2
37 COMET 1,2
38 TOY 1
IS posible configure SQl to show 1,00 o 2,00 when the value have not cents.
When i send to the printer i use this line :
string preco = vercup.Rows[i]["unitario"].ToString();
how can i force to show 1,00 when the values comes 1.
How you store the data isn't related to how the data is presented. yes, when you present the data you can force it to display two decimals.
select convert(decimal(9,2), price) from table
That's just 1 possible solution.

VB.Net About dividing Currency by an X amount of months

im trying to learn how to made stuff with currency.
For example:
I divide 10.000$ by 12 Months, rounding with 2 decimals i have 833,33 $.
If i multiply 833,33 $ * 12 i got 9999,96 $, so there is 0.04 of possible loss.
Rounding the 9999.96 with 2 decimals of presition i got 10.000 $ but that's what i don't want since 0.04 is a loss.
Im using SQL Compact 4.0 as database, the price_month table is decimal(18,2)
Here is my code:
Dim price as Decimal = 10000
Dim pricemonth as Decimal = Math.round((price/12),2) ' 833.33
Console.Writeline(pricemonth*12) ' 9999.96
Console.Writeline(Math.round((pricemonth*12),2)) ' 10000
Any advice how to increase accuracy with currency? Thanks and have a nice day!
Don't round your calculation. Leave the original numbers untouched but when you display the answer round it so that it looks nice.

How to store decimal values in SQL Server?

I'm trying to figure out decimal data type of a column in the SQL Server. I need to be able to store values like 15.5, 26.9, 24.7, 9.8, etc
I assigned decimal(18, 0) to the column data type but this not allowing me to store these values.
What is the right way to do this?
DECIMAL(18,0) will allow 0 digits after the decimal point.
Use something like DECIMAL(18,4) instead that should do just fine!
That gives you a total of 18 digits, 4 of which after the decimal point (and 14 before the decimal point).
You should use is as follows:
DECIMAL(m,a)
m is the number of total digits your decimal can have.
a is the max number of digits you can have after the decimal point.
http://www.tsqltutorials.com/datatypes.php has descriptions for all the datatypes.
The settings for Decimal are its precision and scale or in normal language, how many digits can a number have and how many digits do you want to have to the right of the decimal point.
So if you put PI into a Decimal(18,0) it will be recorded as 3?
If you put PI into a Decimal(18,2) it will be recorded as 3.14?
If you put PI into Decimal(18,10) be recorded as 3.1415926535.
For most of the time, I use decimal(9,2) which takes the least storage (5 bytes) in sql decimal type.
Precision => Storage bytes
1 - 9 => 5
10-19 => 9
20-28 => 13
29-38 => 17
It can store from 0 up to 9 999 999.99 (7 digit infront + 2 digit behind decimal point = total 9 digit), which is big enough for most of the values.
You can try this
decimal(18,1)
The length of numbers should be totally 18. The length of numbers after the decimal point should be 1 only and not more than that.
In MySQL DB decimal(4,2) allows entering only a total of 4 digits. As you see in decimal(4,2), it means you can enter a total of 4 digits out of which two digits are meant for keeping after the decimal point.
So, if you enter 100.0 in MySQL database, it will show an error like "Out of Range Value for column".
So, you can enter in this range only: from 00.00 to 99.99.
The other answers are right. Assuming your examples reflect the full range of possibilities what you want is DECIMAL(3, 1). Or, DECIMAL(14, 1) will allow a total of 14 digits. It's your job to think about what's enough.
request.input("name", sql.Decimal, 155.33) // decimal(18, 0)
request.input("name", sql.Decimal(10), 155.33) // decimal(10, 0)
request.input("name", sql.Decimal(10, 2), 155.33) // decimal(10, 2)