sql datatype rounding - sql

I am having the below query.
declare
#a NUMERIC(20,4)
set #a=24900*0.3333333333
select #a
I am expecting 8299.999999 but it gives 8300.00. Can some body help me on this?

Your result is rounded as you have only 4 decimals at your disposal. Use
set #a = round(24900*0.3333333333, 4, 1)
This will not round but truncate the result.
I do not know if that is what you want, because a value like 0.12346 will be truncated to 0.1234 and not rounded to 0.1235.

you may want this or something like this:
declare #a NUMERIC(20,6)
set #a=cast(24900 as numeric(20,6))*cast(0.3333333333 as numeric(20,10))
select #a

Related

Sum function in Redshift ignoring decimal values

I have a column named myh. It has decimal values like: 65.20, 56.49
When I tried to sum up in Redshift using sum(), it is showing the result as 121 and it is ignoring the decimal values.
Is there any solution to get decimals also?
You can use Round function for addition of values. Also you can use CAST
DECLARE #A DECIMAL(13,5)
DECLARE #B DECIMAL(13,5)
SET #A = 65.20
SET #B = 56.49
SELECT CAST(#A+#B AS decimal)
SELECT ROUND(SUM(#A+#B),2)
Have you tried to Cast it?
Example:
SELECT SUM(CAST(myh as decimal(10,2)))

Wrong calculation of values after applying the round off in SQL Server

have three values a,b,c with datatype Decimal(15,7).
a is a summation of values b and c.
I didn't get what is wrong. What is possible way to get right result after rounding?
For example:
Declare #a decimal(15,7),
#b decimal(15,7),
#c decimal(15,7)
SET #a = '15212.82856500000'
SET #b = '15207.52909500000'
SET #c = '5.29947000000'
In above condition it returns the right result
select #a, #b + #c
But I used the round off 3 to all values it returns incorrect result.
select ROUND(#a, 3), ROUND(#b, 3) + ROUND(#c, 3)
Expected result is
ROUND(#a, 3) = ROUND(#b, 3) + ROUND(#c, 3)
You should round the result, not each variable individually:
SELECT ROUND(#a + #b + #c, 3)
The exact query should be
select ROUND(#a,3),ROUND(#b+#c,3)
This can gives you the desired result.
Each time you ROUND(), you deviating from the actual by a certain percentage, however tiny. Now the more you use the ROUND(), the further away you are deviating. It is always advisable to ROUND the final output.
I'm assuming the example numbers that you gave are not the actual numbers you're having issues with ?

Convert binary to varchar

I need to change the binary output to varchar(string), but it must same as to be the binary output. But while converting the binary(hex) value to varchar, empty set will be returned. Kindly help me.
E.x
If this is my binary value 0x24000000008B0100000000.
I need the same 0x24000000008B0100000000 output after converting it into string.
declare #val binary(20)
set #val=0x24000000008B0100000000
select #val, CONVERT(varchar(max),#val,1)
this also works for me :
SELECT CONVERT(VARCHAR(1000), varbinary_val, 1);
just change your value with varbinary_val
should work like:
DECLARE #a BINARY(20) = 0x24000000008B0100000000
SELECT CONVERT(varchar(max),#a,1), #a

Padding numeric on SQL SYBASE

I have a numeric field like 1,3065 and I need that to became like this: 000000000000000130.
I mean 16 integers and 2 decimals, without the comma, adding 0es to the left and, if needed, to the right.
Is there any way to do that with a query?
I think this will work in Sybase:
select right(replicate('0', 16) + cast(cast(field*100 as int) as varchar(255)), 16)
Try this way:
select REPLICATE('0',16-len(cast(cast((1.3065*100) as int) as varchar(16))))+cast(cast((1.3065*100) as int) as varchar(16))
Result of above select:
0000000000000130
If you want approximation you should use FLOOR or CEILING function as below
declare #multiplier int
select #multiplier = 1000
select REPLICATE('0',16-len(cast((floor(1.3065*#multiplier)) as varchar(16))))+cast(floor(1.3065*#multiplier) as varchar(16))
Result of above select:
0000000000001306
You can change an approximation by changing #multiplier. For example if you want result from 1st query you should change #multiplier to 100.

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