SQL Server, division returns zero - sql

Here is the code I'm using in the example:
PRINT #set1
PRINT #set2
SET #weight= #set1 / #set2;
PRINT #weight
Here is the result:
47
638
0
I would like to know why it's returning 0 instead of 0,073667712

Either declare set1 and set2 as floats instead of integers or cast them to floats as part of the calculation:
SET #weight= CAST(#set1 AS float) / CAST(#set2 AS float);

When you use only integers in a division, you will get integer division. When you use (at least one) double or float, you will get floating point division (and the answer you want to get).
So you can
declare one or both of the variables as float/double
cast one or both of the variables to float/double.
Do not just cast the result of the integer division to double: the division was already performed as integer division, so the numbers behind the decimal are already lost.

Simply mutiply the bottom of the division by 1.0 (or as many decimal places as you want)
PRINT #set1
PRINT #set2
SET #weight= #set1 / #set2 *1.00000;
PRINT #weight

Because it's an integer. You need to declare them as floating point numbers or decimals, or cast to such in the calculation.

if you declare it as float or any decimal format it will display
0
only
E.g :
declare #weight float;
SET #weight= 47 / 638; PRINT #weight
Output : 0
If you want the output as
0.073667712
E.g
declare #weight float;
SET #weight= 47.000000000 / 638.000000000; PRINT #weight

In SQL Server direct division of two integer returns integer even if the result should be the float. There is an example below to get it across:
--1--
declare #weird_number_float float
set #weird_number_float=22/7
select #weird_number_float
--2--
declare #weird_number_decimal decimal(18,10)
set #weird_number_decimal=22/7
select #weird_number_decimal
--3--
declare #weird_number_numeric numeric
set #weird_number_numeric=22/7
select #weird_number_numeric
--Right way
declare #weird_number float
set #weird_number=cast(22 as float)/cast(7 as float)
select #weird_number
Just last block will return the 3,14285714285714. In spite of the second block defined with right precision the result will be 3.00000.

Related

SQL 2017 rounds of decimal number using "FLOOR" function

I have been calculating different integer percentages with different numbers but each time I get floor rounded number. select 13*100/60 gives me 21 and the actual number is 21.66 which using a round function should give us 22 but it can only give me 21 for all different decimal numbers.
I am using SQL 2017. please help
This is due to the fact that you are dividing ints and not floating-point numbers. Integer division returns an integer.
Try the following instead (noting the .0 on the end of the 60):
SELECT 13 * 100 / 60.0
Making one of the components a floating-point number will automatically output the result as a floating-point number.
Output:
21.666666
Incidentally, if you are working with variables and one of them is a FLOAT, it will automatically produce the output you expect:
DECLARE #A FLOAT
DECLARE #B INT
DECLARE #C INT
SET #A = 13
SET #B = 100
SET #C = 60
SELECT #A * #B / #C
Output:
21.6666666666667

How to get decimal portion of a number in a decimal format in SQL Server

I am trying to get remainder part from a decimal number. I have used PARSENAME function like:
DECLARE #Total decimal(7,2) = 1000.40;
DECLARE #Remainder int = 0;
SET #Remainder = PARSENAME(#Total, 1)
It is returning integer form of the decimal portion like 40. How can I get the remainder in decimal form like 0.40. Because I have to store the remainder value as a decimal.
Thanks
Partha
The mod function in SQL Server is % and anything MOD 1 is just the decimal remainder.
SELECT DecimalPortion = 1000.40 % 1
DECLARE #Total decimal(7,2) = 1000.40
DECLARE #Remainder decimal(2,2)
SET #Remainder = #Total - FLOOR(#Total)
SELECT #Remainder
simply subtracting the FLOOR of the input variable will give you the decimal portion
also per Tab's comment you do have the wrong data type for the #Remainder it should be a decimal. If you want it as an integer there are some other methods of accomplishing that too.

Round off to the smallest integer value

I'm using this query to round off the numbers and this round off the next value. Now I need to round off to the before value means if the value is 45.67 then the value should be 45. I tried these two queries and still I need to tweak the values.
Method1:
parsename('$' + convert(varchar,convert(money,round(sum(Column1 * Column2),0)),1),2)
Method2:
parsename('$' + convert(varchar,convert(money,floor(Column1 * Column2),0),1),2)
Really appreciate any suggestions.
The CEILING function returns the smallest integer greater than or equal to the specified numeric expression. The FLOOR function returns the largest integer less than or equal to the specified numeric expression. For example, in considering a numeric expression of 12.9273, CEILING returns 13 and FLOOR returns 12. The return value of both FLOOR and CEILING has the same data type as the input numeric expression.
SELECT CEILING(12.9273);
Here is the result set.
13
SELECT FLOOR(12.9273);
Here is the result set.
12
http://technet.microsoft.com/en-us/library/ms190927%28v=sql.105%29.aspx
To round down you can use FLOOR()
E.G.
DECLARE #number numeric(5,2)
SET #number = 45.67
SELECT FLOOR(#number)
You'd get the result 45
With your example, it looks like it's already working?
declare #number1 numeric(5,2)
declare #number2 numeric(5,2)
set #number1 = 1.23
set #number2 = 21.69
select parsename('$' + convert(varchar,convert(money,floor(#number1 * #number2),0),1),2)
select #number1 * #number2
Results
$26
26.6787

CEILING returns FLOOR result - SQL SERVER 2008 R2

DECLARE #a int;
DECLARE #b int;
SET #a = 9;
SET #b = 2;
SELECT CEILING (#a/#b);
It is returning as 4 instead of 5. Why?
Edit: I would like to get next smallest integer if the quotient is not whole number.
Try:
SELECT CEILING (#a/CAST(#b AS float))
And consider NULLIF(#b,0) too, to avoid a Division By Zero error.
After dividing 9 by 2 a decimal fraction is Truncated to its integer part - 4, not Rounded to 5. Try:
SELECT 9/2
Resilt is 4. Then CEILING(4) = 4
To get next integer declare variables as data types that can handle decimal part: NUMERIC,FLOAT, REAL.
SQL Server does integer division. So 9/2 = 4 in SQL Server.
Taking the ceiling of an integer is the same integer.

Splitting decimal in sql

I am getting result as decimal in stored procedure. If I am getting result as 123.45,
I want to split it into 123 and 45. Can anybody help?
use SQL function FLOOR() for getting integer part
and subtract that from the original for the decimal part
You can also make use of ROUND instead of FLOOR.
See section C. Using ROUND to truncate for trucate, and then subtract that from the original.
Be aware that using FLOOR on negative numbers might not give you the required result.
Have a look at this example
DECLARE #Dec DECIMAL(12,8)
SET #Dec = -123.45
SELECT FLOOR(#DEc)
select round(#Dec, 0, 1)
try this;
DECLARE #result DECIMAL(8,2) = 123.45
SELECT CAST(round(#result,0) AS FLOAT)
SELECT REPLACE(#result % 1 ,'0.','')
OR
DECLARE #result decimal(8,2) = 123.45
select PARSENAME(#result, 2) AS LeftSideValue, PARSENAME(#result, 1) AS RightSideValue