简体   繁体   中英

Round Function in SQl

select Round((10*100)/115,0)

Result : 8

But Result : 8.69

But i want to display 9

I tried below query also, but result is same..

select Round((10*100)/115,0)

Please solve my prob....

Thanks

Try below

declare @n decimal(4,1)
select @n = 8.69

select case when PARSENAME(@n,1)>=5 then ceiling(@n) else floor(@n) end 

idea is if the number after point is greater than 5 then go to upper value else go to lower one

If your database is oracle then the following function you can use..

For result 9 use ceil function and For result 8 use floor function

select ceil(8.69) val from dual;

  select floor(8.69) val from dual;

对于较高的值,您可以使用Ceil函数,对于较低的值,您可以在oracle中使用floor函数,如下所示

select ceil(8.69) Upper_val,floor(8.69) Lower_val from dual;

It returns an integer. Try to convert it to a decimal before calculation

Eg.

select Round((10*100)/115,0)

change to

select Round((10.0*100)/115,0)

Hope this will help

If Its SQL Server please do try this:

SELECT CEILING(10.1) AS UpperValue, FLOOR(10.6) AS LowerValue, 
Round(CAST((CAST(10 AS float) * CAST(100 AS float)) / CAST(115 AS float) AS float), 0)
AS YourValue, Round((10*100)/115,0) AS YourOldValue;

Here is what's happening:

Even before using ROUND Function

SELECT 1000/115 /* the result is 8 */

and try this

SELECT ROUND(1000.0/15, 0) /* the result is 9.000000 */

and if use this

SELECT CEILING(1000.0/115) /* the result is 9 */

so basically before call any function the result is 8.

you can use this:

SELECT ROUND(CONVERT(decimal, 1000) / 115, 0)

or simply this:

SELECT ROUND(1000.0/15, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM