I am trying to divide one number by another in a SQL view. I'm dividing two columns that are each of type int, and the problem is that its giving me a rounded output, rather than an accurate one.
Here is my select:
SELECT numberOne, numberTwo, (numberOne*100/NULLIF(numberTwo, 0)) as PctOutput
This is working, but when it dives 3 by 37 its giving me 8 instead of 8.108.
How would I modify this to give me an accurate answer? Do I need to cast the numbers out of ints? if so - into what?
Try an implicit cast:
SELECT
numberOne,
numberTwo,
((1.0*numberOne)*100/NULLIF(1.0*numberTwo, 0)) as PctOutput
**--Cast the denominator as Float**
SELECT 3 * 100 / NULLIF(CAST(37 AS FLOAT),0) -- 8.10810810810811
**--Multiply by 1.0 in either numerator OR denominator**
SELECT 3 * 100 / NULLIF(37 * 1.0,0) -- 8.108108
SELECT 3.0 * 100 / NULLIF(37,0) -- 8.108108
**--Convert it to decimal**
SELECT CONVERT(DECIMAL(25,13), 3) * 100 /
NULLIF(CONVERT(DECIMAL(25,13), 37),0) -- 8.108108108
You need to cast the numbers from INT into either float or decimal.
If you use literals, they will likely be decimal (NUMERIC), not float (http://stackoverflow.com/questions/1072806/sql-server-calculation-with-numeric-literals)
Note that if you use decimal, you should be aware of the rules of scale and precision in division if you have numbers near the boundaries where you might lose precision:
http://msdn.microsoft.com/en-us/library/ms190476.aspx
In SQL Server the result is always of the same type as the inputs. So yes you do need to convert the inputs.
I generally convert using convert(decimal(9,2),numberOne) but depending you may want to convert(real,numberOne) or use a different level of precision.
Related
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6
I'm using CAST AS DECIMAL and specifying the precision I require. Here is the snippet from my SQL query:
CAST(((num/volume)*100) AS decimal(16,8)) AS impact
No matter what I put in the parameter for decimal, the value in the impact column is always '0.00'.
This should not be the case as I have a num = 5, a volume = 81000, so this should give me '0.0061' in impact.
Please can someone explain why CAST AS DECIMAL isn't accepting my parameters to give the precision I require in the impact column?
because int/int result cast as int and thats why 5 / 81000 equal 0
you can use
(CAST(num AS decimal(16,8)) / CAST(volume AS decimal(16,8))) * 100 AS impact
It is likely because your two parameters for num and volume are integers. Cast one of those say as a decimal and you should get the answer you want.
eg
select CAST(((cast(num as decimal(5,2))/volume)*100) AS decimal(16,8)) AS impact
Your database is clearly doing integer division. I would just rewrite this as:
CAST( (num * 100.0 / volume) AS decimal(16, 8)) AS impact
I have a sql query (SQL server 2005) that's creating a var and doing some math. The math works when the ticket count is 0 or 250000, but it's not creating a decimal point when the ticket count is any other value. (It reads 0.) Here is the query -
SELECT ticketCount, ((250000 - ticketCount) / 250000) * 100 AS percentSold
FROM raffleTickets
Where ticketCount in the DB is how many tickets of 250000 remain to be sold. If ticketCount is 250000, percentsold is 0, which is correct. If ticketCount is 0, percentSold is 100, which is correct. For all other values, percentSold is returning 0.
Please help! Thanks.
SQL Server does integer division (this varies among databases).
You can easily fix this by putting a decimal point after the constants:
SELECT ticketCount, ((250000.0 - ticketCount) / 250000.0) * 100.0 AS percentSold
FROM raffleTickets;
If you want the integer portion, then you can cast() the result back to an integer. Alternatively, you can use the str() function to convert the value of percentSold to a string with the appropriate number of decimal points.
Your formula is correct,
PercentSold = (TotalTickets - TicketsRemaining) / TotalTickets
But what is the Domain and Range of the above function? Your numbers are all expressed as integer, but you probably want to calculate using Real.
PercentSold = ( ( TotalTickets - TicketsRemaining )*100.0) / (TotalTickets)
This forces the calculation to be done in Real. Languages that know how to declare variables and calculations in specific types (i.e. Domains) can make this clearer, more apparent.
Wanting to represent a real number with a certain precision means you want to perform output formatting. SqlServer must have a way to specify output format for numbers in the Real Domain. I defer to SqlZoo.net for their answer to formatting.
The only thing you need to do is putting plus or minus 0.0 in your formula.
SELECT ticketCount, ((250000 - ticketCount - 0.0) / 250000) * 100 AS percentSold
FROM raffleTickets
I am using the following query.
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal).
but it returns the field number 2 result instead of 1.6
Please let me know how can I do it?
Here:
select * from wp_rg_lead_detail where lead_id=5063 and field_number=cast(1.6 as decimal(2, 1))
You must specify number of digits in your decimal when casting, an number of digits after coma (I set them to 2 digits decimal with 1 after coma). You can easily test such casting by simply writing query:
SELECT Cast( 1.6 as decimal(2,1))
this will produce you effect of your casting. If you don't include (2,1) part, it will be automatically rounded to 2.
In Microsoft SQL Server 2005, why do the following commands produce integer results?
SELECT cast(151/6 AS DECIMAL(9,2))
SELECT 151/6
In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you're just dividing two integers and that's expected.
If you cast one of the integers as a decimal BEFORE you do the division, you'll get a decimal result.
SELECT 151/CAST(6 AS DECIMAL (9,2))
Yes that is standard behavior
do
SELECT 151/6.0
or
SELECT 151/(CONVERT(DECIMAL(9,2),6))
or
SELECT 151/(6 * 1.0)
Because 151 and 6 are integers and you are doing integer division, even before the cast.
You need to make sure at least one of the arguments is a float type:
SELECT 151.0/6
Or
SELECT 151/6.0
Not a direct answer to your question. Still worth to take a look at Operators in Expressions if you need this in SSRS
/ Divides two numbers and returns a floating-point result.
\ Divides two numbers and returns an integer result.
Mod Returns the integer remainder of a division.
You need to give a placeholder for decimal places as well
Example
SELECT 151.000000/6
OR
SELECT 151/6.000000
Both will produce
25.16666666
For the same reason they would in C#, Java and other mainstream languages.
In integer arithmetic, the CAST is after the maths...
The CAST statement is a bit verbose. You can use the following instead:
DECLARE #TO_FLOAT FLOAT = 1.0;
SELECT (1 * #TO_FLOAT) / 2;
Or use a different multiplier type like DECIMAL if you prefer.
Try this:
SELECT 1.0*cast(151/6 AS DECIMAL(9,2))
SELECT 1.0*151/6