Transformation of units in SQL Server - sql

I have a dataset, df, that has a column of values that are in MB. I would like to transform into TB.
MB
10000000
20000000
Desired
TB
9.09
18.18
Doing
select MB AS 'TB', (CONVERT([int],round([MB]/((1024)*(1024)),(0)))) AS TB from df
However, the result I get is
MB
0
0
I am still researching. Any suggestion is appreciated

/ is integer division in SQL Server.
It means, that for example
SELECT 4 / 5
will return 0.
But, if you write
SELECT 4 / 5.0
you'll get 0.8
5.0 is treated as decimal type and all values in the expression are converted to decimal and division is no longer integer.
So, you can use 1024.0 constant in the expression, and all the values in it will be converted to decimal type and division will not be integer.
In the question you say that you want to show results with two decimal places, so you should not convert result to int.
select
[MB]
,round([MB]/(1024.0*1024.0), 2) AS TB
from df

Related

snowflake Computation for dividing 2 columns giving wrong values

I am exactly doing this Sum(2322933.99/1161800199.8)*
100
I should get
1.9 something but I am getting 64. Something
can anyone guide my y this division in snowflake giving wrong results
I tried them converting into decimal values and tried with Formula div0()
Nothing worked
I guess that your database table has 33 rows. So you get 33 * 1.9 (because of SUM), which is about 64.
My guess, with the few details that you gave us:
sum(x)/sum(y) is different than sum(x/y)
1/2 + 2/4 + 4/8 = 1.5
(1+2+4)/(2+4+8) = 0.5
Try writing sum(total gross weight)/sum(total cases filled) instead of sum(total gross weight /total cases filled).

I've a column in my pandas dataframe where it contains arithmetic operator (*) as well

The data type of the column in a pandas dataframe is object. It contains an arithmetic expression, for example: 24 * 365. I would like to get the result (24 * 365 = 8760) of the expression returned in place of the expression. Can anyone help in resolving this?
The quantity column in the picture shown is having number of units multiplied by the quantity of each unit. I would like to get the total quantity by multiplying them.
Search for strings that contain ' x ' and split based based off that, convert to a number and multiply the left of the split by the right of that split.
df = pd.DataFrame({'Quantity' : ['1', '1000', '24 x 13.75', '60 x 40', '750']})
df['Quantity1'] = np.where((df['Quantity'].str.contains(' x ')),
pd.to_numeric(df['Quantity'].str.split(' x ').str[0]) * pd.to_numeric(df['Quantity'].str.split(' x ').str[1]),
df['Quantity']).astype(float)
df
#If above doesn't work then delete `.astype(float)` and there may be some additional logic to consider like what if there is a cell in this format 12.25 * 43 * 1 OR what about 40 / 8, or what if there is a capital 'X', etc.
ouput:
Quantity Quantity1
0 1 1.0
1 1000 1000.0
2 24 x 13.75 330.0
3 60 x 40 2400.0
4 750 750.0
inter_m=df["a1"].str.split("*",expand=True).astype('int32')
Split the column by the symbol * . Later convert the numbers to type integer. Store in intermediate dataframe.
df["a1"]=inter_m[0] * inter_m[1]
Multiply the two columns in the intermediate dataframe.

Percentage calculation in SQL Server

I have columns RSL and SUMofRSL. I have tried calculating the percentage but it returns either 100% or 0%. In some instances it is a wrong calculation since it shows 0% . Below are the examples for your reference.
RSL SUMofRSL Percentage
------------------------------
2 2 100%
1 2 0%
48 96 0%
10 10 100%
I have used
([RSL] / [SumOfRSL]) * 100
Assuming the data types of RSL and SumOfRSL are integers you will need to cast the columns to a data type that supports decimal places.
For example:-
(CAST([RSL] AS DECIMAL(10, 4)) / CAST([SumOfRSL] AS DECIMAL(10, 4)))
The division of two Integer factors will be another Integer. At least one of your factors must be decimal type if you want that the result be decimal. (See this link)
You can CAST one or both of your values as Kane suggested.

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)