Round decimal values after decimal to lower decimal - sql

I have value in 1200.719994 and I want to convert it to the 1200.71000
how can I get that in sql view, I tried several rounding , ceiling function but i was not able to have it.
can any one help , please

SELECT ROUND( 1200.719994, 2, 1)
returns 1200.710000. The 1 is the function parameter, anything other than 0 means truncate, rather than round.

Not sure what you mean by "ceiling function but i was not able to have it." CEILING is available in all supported versions SQL Server; if it's not in the version of SQL Server you have, what version are you using? It was even available in SQL Server 2000.
It's not CEILING you're after though, it's FLOOR:
SELECT FLOOR(1200.719994 * 100) / 100;

Related

SQL Division precision

I have 2 columns which I need to divide sum(cola)/sum(ColB), but I am not getting the desired results since SQL server seems to truncate values after decimal
For eg. I have-
select 281370/1035
is giving 271 using simple division, whereas actual result of division is 271.8550724637681 and I want to display 271.8
I tried
SELECT cast(round(281370/1035,1) as numeric(36,1))
but that results 271.0
In SQL Server, you have to cast the integers to decimal and you could use Round to get desired precision.
SELECT cast(Round(CAST(281370 AS decimal) / CAST(1035 AS decimal),1,1) as decimal(10,1))
The problem is that you given the int number and want a decimal result
try this
select convert(decimal(30,10),281370.0/1035.0)
or
select Round(convert(decimal(30,10),281370.0/1035.0),1,1)
#Stormcloak gives the answer to specifically wanting a single position as a mantissa, however to return an exact answer you could "simply" implicitly change the datatype.
select 281370.0/1035
Returns:
271.855072
In Presto DB:
select (CAST(11 as decimal(8,6))/CAST(7 as decimal(8,6))) as result
result:1.571429
decimal(xp,xs)
xp--> total number of digits(before decimal point+ after decimal
point)
xs--> number of digits after the decimal point
reference: https://prestodb.io/docs/current/functions/decimal.html

What is '.' in SQL Server query

I just started working on one existing project and see the query which looks something like this -
select 100. * somecolumn1 / NULLIF(somecolumn2, 0.) AS ColumnValue,
from dbo.SomeTable
I am not sure what is '.' operator in select statement. Could anyone please help me understand?
Also this is just a portion of massive SQL Server query. And if I comment this particular select statement the query runs in about 7 seconds otherwise it takes about 5 minutes to execute the query. Can this statement be optimized?
SQL Server does integer division. So, 1/2 = 0, not 0.5.
To avoid integer division, you want to use values with decimal places. The simplest way to accomplish that is to use numeric literals. I would always include a 0 after (and before) the decimal place, so the value is easier to see:
select 100.0 * somecolumn1 / NULLIF(somecolumn2, 0.0) AS ColumnValue,
from dbo.SomeTable
I often accomplish this by multiplying by 1.0. Alternative, you can use cast() or convert() on an integer column.

SQL Server Cast and Rounding

I have in my select clause:
AVG (cast(scale as decimal(5,2)))
I keep getting a number like: 0.6523412897, nothing I do seems to get me to my desired: 0.65.
Basically I want to have a scale of 2 (two decimal places).
Thanks
Cast the average, don't average the cast:
cast(AVG(scale) as decimal(5,2))
Update
Using ROUND() changes the value not the type. Eg. select round(0.12345,2) returns 0.12000 as it should becuase it rounds 0.12345 to 0.12 but keeps the original type with 5 decimals after the point, hence 0.12000. To change the type one must use cast, as in my post.
Try
ROUND(AVG(scale), 2)
It depends upon the database you are using, but ROUND is the answer for SQL Server:
ROUND

SQL Round function not working, any ideas?

Here is the SELECT statement:
SELECT ROUND(ISNULL(SUM(Price),0),2) As TotalPrice
FROM Inventory
WHERE (DateAdded BETWEEN #StartDate AND #EndDate)
Any ideas of why it's not rounding to two decimal places?
instead of ROUND(ISNULL(SUM(Price),0),2) you could try CAST(ISNULL(SUM(PRICE),0) AS DECIMAL (4,2))
What datatype is Price?
ROUND in BOL
SELECT ROUND(123.4545, 2); -- = 123.4500
GO
SELECT ROUND(123.45, -2); -- = 100,00
GO
The underlying datatype stays the same: you merely round but leave trailing zeros.
For 2 decimal place output, you'd need to CAST to decimal(x, 2)
You might be having marshalling issues for the column in your environment. Might try an explicit cast CAST(ROUND(...) AS NUMERIC(18,4)) or even just try making 0 0.0. Make sure also you are binding the column with the proper datatype in your application.
All the cool people use COALESCE instead of ISNULL. COALESCE is portable and you can have as many parameters as you want (not just two!!)
Anyway I'm not sure if this was just an example but you may also have DA issues with your data if it had not already been rounded at this stage.

SQL Rounding Question

I have the following values being returned in a column.. 0.250000 and 13.000000. I'd like to round the 0.250000 up to 1 and leave the 13 as is. Any easy way to do this since they both fall within the same column?
Your DBMS probably has a CEILING function. Try that.
The ceiling function will work well because you want to round to the nearest whole number. For more methods on rounding with SQL Server....
SQL Server Rounding Methods
select CEILING( columnname ) from tablename