Customize rounding a decimal value in SQL Server - sql-server-2012

I have a list of values in SQL Server which need some customized rounding. I have tried with inbuilt SQL Server functions but those functions didn't work.
I want to round a decimal value to nearest 5 value
For example
1.02,1.01,1.03,1.04,1.05 -- should be rounded to 1.05
1.06,1.07,1.08,1.09,1.10 -- should be rounded to 1.10
1.11,1.12,1.13,1.14,1.15 -- should be rounded to 1.15
1.16,1.17,1.18,1.19,1.20 -- should be rounded to 1.20
Could you please suggest a working solution for this scenario in SQL Server?
Thanks in advance
Raju

Are you only ever going to deal with two decimal digits?
If so, you could use a function like this:
CREATE FUNCTION dbo.RoundToFive(#Input DECIMAL(16,2))
RETURNS DECIMAL(16,2)
AS
BEGIN
RETURN ROUND(#Input*20 + 0.49, 0) / 20.0;
END;
If you use this like this:
DECLARE #data TABLE (StartValue DECIMAL(16,2))
INSERT INTO #data (StartValue)
VALUES (1.00), (1.02), (1.01), (1.03), (1.04), (1.05), (1.06)
SELECT StartValue, dbo.RoundToFive(StartValue)
FROM #data
you'll get an output like this:

Related

How to multiply string value to longint in SQL

I have the below data which I want to multiply together, column A times column B to get column C.
A has datatype string and B has datatype long.
A B
16% 894
15% 200
I have tried this expression in query cast(A as int)*B but it is giving me an error.
You can try below way -
select cast(left(A, patindex('%[^0-9]%', A+'.') - 1) as int)*B
from tablename
You need to remove the '%' symbol before attempting your cast. And assuming you are actually wanting to calculate the percentage, then you also need to divide by 100.00.
cast(replace(A,'%','') as int)/100.00*B
Note: You need to use 100.00 rather than 100 to force decimal arithmetic instead of integer. Or you could cast as decimal(9,2) instead of int - either way ensures you get an accurate result.
You may well want to reduce the number of decimal points returned, in which case cast it back to your desired datatype e.g.
cast(cast(replace(A,'%','') as int)/100.00*# as decimal(9,2))
Note: decimal(9,2) is just an example - you would use whatever precision and scale you need.
The syntax of the cast in SQL Server is CAST(expression AS TYPE);
As you cannot convert '%' to an integer so you have to replace that with an empty character
as below:
SELECT cast(replace(A,'%','') AS int);
Finally you can write as below:
SELECT (cast(replace(A,'%','') AS int)/100.00)*B as C;

How to show decimal point in hive?

I want to display the decimal precision along with the result for decimal datatype in hive. However if there is no fraction part , in hive it will not display the decimal points.
hive> select cast(11 as decimal(14,2));
11
hive> select cast(11.22 as decimal(14,2));
11.22
In the above example instead of 11 it should display 11.00.How to achieve this?
Please help.
The following format_number() function should do it.Source, return type will be string though.
select format_number(11,2)
Note: Precision and Scale were added from Hive 0.13.
Use round or float_number to set the decimal point where u like , example :
select round(SUM(150.100 + 127.0090), 2);
or
select float_number(var,2);

how to get only 2 digits after decimal point?

SQL Server 2012
My output is 24.242553, but I need only 2 digits after my decimal point.
Here is my query :
AVG(cast(dbo.vw_NEW_UG.SCORE_A20 as decimal))AS ACT_SUPER_SCORE
I also tried this, but its not working.
AVG(cast(dbo.vw_NEW_UG.SCORE_A20 as decimal(10,2)))AS ACT_SUPER_SCORE
Thanks.
You can use ROUND function like this
CAST( ROUND(AVG(cast(dbo.vw_NEW_UG.SCORE_A20 as decimal) ) , 2 ) AS decimal(18,2))
Move your cast to the outside of your average function and try that:
cast(AVG(dbo.vw_NEW_UG.SCORE_A20) as decimal)AS ACT_SUPER_SCORE

how to retrieve values that are fractional from a column in SQL

I have a column named quantity with values 1.00,2.00,1.5,2.5,etc. If I want to retrieve the values 1.5,2.5 along with all other similar values how can I write the query?
in SQL in have tried:
SELECT quantity_billed FROM invoice_line_item WHERE quantity_billed != ROUND(quantity_billed)
and
with like operator it is giving the values like 1.00 also.
You can use a MODULO function, too:
SELECT quantity_billed
FROM invoice_line_item
WHERE quantity_billed MOD 1 <> 0
%LIKE% only works for Character string,so you should convert to varchar.if ur data in float or decimal.
SELECT quantity_billed
FROM invoice_line_item WHERE convert (varchar,quantity_billed )
like '%.5%'
How about using NOT LIKE('%.00') to get fractional amounts. Not sure which database you are using so you may need to modify slightly but this works in MySql and SQL Server

SQL Convert float to comma varchar with decimals

I have a couple floats that are kinda big. They're around a 100 million.
I would like this number to show like the following 123,456,789.01234
I've found that I can use CONVERT if its a money datatype but this doesn't do the full trick (it leaves off some decimal places).
I have to have commas on the left and five decimal places on the right.
Is there any built in SQL function to help with this? Or do I have to write a custom function?
Thanks
*** Update
I forgot to mention that I'm just displaying these as varchars. So there isn't any calculations after this.
This is running on an SQL database so MySQL and Oracle won't work.
DECLARE #f FLOAT
SET #f = 123456789.01234
SELECT LEFT('$' + CONVERT(VARCHAR(20), CAST(#f AS MONEY), 1), LEN(#f) - 2)
this will cut it up to two places of decimal for formatting. You can change LEN(#f) - 2 to modify this setting.
if you are just displaying this as text you can do the following:
oracle :
select to_char(123456789.01234,'999,999,999.99999') from dual; => 123,456,789.01234
MySQL :
select format(123456789.01234,5) => 123,456,789.01234<br>
the MySQL function rounds