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

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.

Related

Find next greater number with zeros in sql number

I'm looking for a way to find next greater number starting by 1 and followed by zeros in Microsoft SQL. Numbers could vary in digits. ie:
Query: 9856, Result after procedure: 10000
Query: 98999, Result after procedure: 100000
Thanks.
EDIT: There is no chance of having negative numbers. This is a calculation for a energy meter. For example, numbers can go up to 99999 or 999999 or 9999999. When energy overcome that number, it will start again at 0. So I can't read what energy has been used in that period. To know it, I need to calculate the number as asked, then perform some basic maths.
There is no need for knowing what is going on on 10, 100, etc, because of the nature of the operation. It will only be used when the above escenario happend.
I don't know why you require or mathematically any other formula can be implemented. but technically that can be achieved as follows
DECLARE #count INT
SET #count = 1000
DECLARE #result INT
SET #result = CASE WHEN #count%10 = 0 THEN #count ELSE CAST('1'+REPLICATE('0',LEN(#count)) AS INT) end
SELECT #result
This works for positive numbers (numbers greater than zero):
select power(10, ceiling( log10(the_number) )) from mytable;
In case the number is already a power of ten (1, 10, 100, ...) , the number itself is returned.
You can do this with just arithmetic operations:
select power(10, floor(log(v.n - 0.1, 10)) + 1)
from (values (1), (10), (8), (9982), (124)) v(n)
This is a fairly crude way of doing it, however it does work. The query basically looks at the number of digits your number has, assumes the next integer you want starts with a 1 and then adds the relevant number of 0's to it.
Note this only looks for the next increment and does not round down.
Also for 10 you will get 100 and for 1000 you will get 10000.
declare #number int = 98999;
declare #len int = len(#number);
declare #stringtoreturn nvarchar(200)='1';
declare #runs int = 1;
while #runs<=#len
begin
select #stringtoreturn = #stringtoreturn + '0';
select #runs=#runs+1;
end
select #stringtoreturn;

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

SQL Server, division returns zero

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.