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
Related
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;
Instead of rounding off the value and avoiding decimals, Is there any other way to make it only whole value.
Your use case sounds like the FLOOR() function, which 'truncates' a number by removing all decimal values from the result:
SELECT FLOOR(1.9999999)
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.
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
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.