How to round multiple numeric columns to 2 digits in sql? - sql

Say if I need to round multiple numbers to 2 digits now, but I don't want to repeat using round(..., 2) or format(...).
Is there any method to set up the float numbers with 2 digits globally?

select cast(float_column as decimal(10,2))
from your_table

Declare the column or variable of type numeric(18, 2)
You can also use with the CONVERT function
select CONVERT(numeric(18, 2) , 5.54722)

Related

MSSQL - Getting decimals in a division [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Decimal values in SQL for dividing results
(6 answers)
Closed 6 years ago.
I'm using MSSQL 2014 and I'd like to know if there is a better way to display 2 decimals in a simple division like 10/3.
SELECT 10/3 -- returns 3
SELECT CONVERT(DECIMAL(10, 2), 10/3) -- RETURNS 3
SELECT CAST(10/3 AS DECIMAL(10,2)) -- RETURNS 3
The only way I found to make it work is by casting the divisor as float:
SELECT 10/CAST(3 AS FLOAT) -- returns 3.333333...
SELECT CONVERT(DECIMAL(10, 2), 10/CAST(3 AS FLOAT)) -- RETURNS 3.33
SELECT CAST(10/CAST(3 AS FLOAT) AS DECIMAL(10,2)) -- RETURNS 3.33
Are these two last options the best approach available?. Is it possible to do this without any cast/conversion?
Yeah, to truncate you have to specify that you only want two digits. If it were me I'd do:
SELECT cast(10/3.0 as decimal(10,2))
I get a float if I do,
select 10/3.0
https://stackoverflow.com/a/11719098/28045
Division of one integer by another will result in an integer. Either cast the numbers or use variables of float or decimal type having values of 10 and 3.

How to format % and in 2 decimal points?

How do I code format the return data in 2 decimals and with percentage format like 100.00% or 67.39% instead of 100.000000 or 67.391304?
SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46 as 'C%'
I tried ROUND() but I got the error stating that the round function requires 2 to 3 arguments?
ROUND(SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) as 'C%'
Thanks!
You can convert to a decimal your original value:
CONVERT(VARCHAR(20), CONVERT(DECIMAL(18,2), SUM(qa.scripting1+qa.conduct1+qa.conduct2+qa.conduct3)*100.0/46) ) + '%' as 'C%'
The first number in the decimal represents the number of digits in the number including decimal places, and the second number represents the number of decimal places.
You should pass number of decimals in second parameter to round function. For formating you can cast number to money and then cast to varchar:
select cast(cast(ROUND(SUM(123.12321)*100.0/46, 2) as money) as varchar) + '%'
Using Round and Cast will work. First round to 2 decimal places then convert to a decimal with 2 places to truncate the excess zeros.
select cast(Round(yourValue, 2) as decimal(18,2))
Sql Fiddle
You can use Format function
select FORMAT(100.0000, 'N' , 'en-us')
returns 100.00
and
select FORMAT(67.391304, 'N' , 'en-us')
returns 67.39
EDIT
In version below 2012 you can do this
SELECT CAST(67.391304 AS NUMERIC(10, 2))
returns 67.39
You can just do:
select FORMAT(0.391304, '##0.00%')
But keep in mind that it implicitly multiplies by 100, so the above will display as 39.13%.

How to round a number upto n decimal places in sql server

I have column in table which I want to round upto some decimal places.
Which is stored in some other table.
Actually my data is stored as 8 decimal places, but based on user preference, I need to show upto that decimal place.
Column datatype is numeric(19,8)
I have procedure which is returning all rows. So At that time only I want to round my column up to specified decimal decimal
Want to do something like
declare #i int = 2
Begin
select cast (12533333.193 as numeric(38,#i))
end
how to achieve this?
Please suggest
This is the MOST easy of all
Before starting I want to tell that this works only for float type (Yes! you need to convert your decimals / numerics to float!). Syntax wise it is Most Readable as well!
select round(cast(65.456 as float),2) (This will round off to 2 decimal places)
select round(cast(65.456 as float),1) (This will round off to 1 decimal places)
select round(cast(65.456 as float),0) (This will round off to 0 decimal places)
See the snapshot below
You can just use the ROUND function for sql-server
DECLARE #i int = 2
BEGIN
SELECT ROUND(val,#i)
FROM mytable
END
SQLFIDDLE
you can cast to number with 2 decimal after round up in query,
or when you display at report or system
DECLARE #i int = 2
BEGIN
SELECT
cast(
ROUND(val,#i)
as decimal(10,2)
)
FROM mytable
END

SQL number decimals

I run a query and all my numbers are coming out to 5 decimal points:
for example -
156713.55000
2103613.03000
2080.08000
is there a simple piece of code I can add into my code so the 'Cost' table results are to 2 decimal points?
Following example will help you.
With rounding:
select ROUND(55.4567, 2, 0)
-- Returns 55.4600
select CAST(55.4567 as decimal(38, 2))
-- Returns 55.46
Without rounding:
select ROUND(55.4567, 2, 1)
-- Returns 55.4500
select CAST(ROUND(55.4567, 2, 1) as decimal(38, 2))
-- Returns 55.45
or
Use Str() Function. It takes three arguments(the number, the number total characters to display, and the number of decimal places to display
Select Str(12345.6789, 12, 3)
displays: ' 12345.679' ( 3 spaces, 5 digits 12345, a decimal point, and three decimal digits (679). - it rounds if it has to truncate
for a Total of 12 characters, with 3 to the right of decimal point.
Just use the ROUND function:
SELECT ROUND(column, 2) FROM Cost
Or to strip the decimals and round, use CAST:
SELECT CAST(column as decimal(10, 2))

How to give select digit after decimal point?

In database there is column amount which datatype is money. I want to select that row only with two digit after decimal. for this how to write the query?
My query is like this:
SELECT AMOUNT FROM DETAIL_PAGE.
I want to modify this query so that it selects two digits after decimal point.
SELECT AMOUNT - FLOOR(AMOUNT) FROM DETAIL_PAGE
That will get you just the decimal though. I think you want
SELECT FORMAT(AMOUNT, 2) FROM DETAIL_PAGE
Or without commas:
SELECT REPLACE(FORMAT(AMOUNT, 2), ',', '') FROM DETAIL_PAGE
Not sure if this is SQL standard and works elsewhere, but in Oracle you can say
select round(amount,2) from detail_page
-- round(12.345, 2) would return 12.35
or
select trunc(amount,2) from detail_page
-- trunc(12.345, 2) would return 12.34