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;
for upper value you can use Ceil function and for lower value you can use floor function in oracle as below
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
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)
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;
Related
How can I Round-up or Round-down the SpecialOfferPrice column in this query?
SELECT TB_Product. ProductID,
TB_Product.RetailPrice * 0.95 AS SpecialOfferPrice
Use the below query.
SELECT TB_Product. ProductID,
round(TB_Product.RetailPrice * 0.95) AS SpecialOfferPrice
for more information refer to this [link]https://www.w3schools.com/sql/func_sqlserver_round.asp
Try to use CEILING(numeric) to round up.
According to MSDN:
This function returns the smallest integer greater than, or equal to,
the specified numeric expression.
SELECT
CEILING(TB_Product.RetailPrice * 0.95) AS SpecialOfferPrice
To round down use ROUND.
As MSDN says::
Returns a numeric value, rounded to the specified length or precision.
SELECT ROUND(175.45, 0)
Output is:175.00
UPDATE:
If number is below 200 and you want to round to only one number after the decimal point and if it is above 200, you want to use use CEILING:
DECLARE #delimiter DECIMAL(10,5) = 200
SELECT
CASE
WHEN E.FooNumber < #delimiter THEN ROUND(e.FooNumber, 0)
ELSE CEILING(e.FooNumber)
END AS FooNumbers
FROM (VALUES(100.1),
(180.4),
(250.5),
(350.8)) E(FooNumber)
Try this :
SELECT TB_Product. ProductID,
ROUND(TB_Product.RetailPrice * 0.95, -2) AS SpecialOfferPrice
Use the CEILING(), FLOOR() functions.
This link goes further in explaination (https://www.mssqltips.com/sqlservertip/1589/sql-server-rounding-functions--round-ceiling-and-floor/).
You can use the FLOOR() and CEILING() functions to round up or down respectively.
https://learn.microsoft.com/en-us/sql/t-sql/functions/ceiling-transact-sql?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/t-sql/functions/floor-transact-sql?view=sql-server-ver15
I have a function that calculates number of days
select (x * 0,5) from dual.
my problem is tha when x=1 i get .5 instead of 0,5.What i want to get is numbers in format 0.5,1,1.5,2,2.5 ..
Could you please give me a help with this.
I know this is old but I did the following, where x is datatype NUMBER(20,3)
CASE WHEN x < 1 THEN '0'
ELSE '' END
|| to_char(x)
I didn't want any trailing zeros but did want a leading zero if x was, say, 0.99 or 0.5
So I'm just using the regular to_char and adding a leading zero if x is less than 1.
Try to use:
select TO_CHAR(x * 0,5, '990,99') from dual
EDIT:
Try to create a function like this:
create function getDecimalFormat(x in number)
return varchar2
as
begin
return RTRIM(TO_CHAR(x, 'FM999999999999990.99'), '.');
end;
If you always want 1 decimal place use to_char to round
select TO_CHAR(28.324, '90.9') from dual;
returns
28.3
and
select TO_CHAR(28, '90.9') from dual;
returns
28.0
Hope this helps
I what to know if there is equivalent function of roundup of excel in sql query?
Especially for roundup(value,2)
in SQL using round:
select cast(ROUND(350.00/24,2) as decimal(18,2))
result = 14.58
in Excel using roundup
result = 14.59
How can I achieve excel's roundup result in sql?
There is:
select round(12.4343423, 2)
first = number
second = decimal places
OR
SELECT CEILING(111.45) becomes 112
You can do excel like round-up by this query:
SELECT CEILING( (350.00/24) * 100 ) / 100
Result 14.59
You can also write a TSQL function to roundup like excel( as Anthony, answered to similar question) :
CREATE FUNCTION RoundUp(#value float, #places int)
RETURNS float
AS
BEGIN
RETURN SELECT CEILING(#value * POWER(10, #places)) / POWER(10, #places)
END
GO
And you can call this function like this:
SELECT dbo.RoundUp(350.00/24, 2)
Result 14.59
I got a output like this
0.00234690616839645663803848176618444236941
the way I want is
2.3
first I remove the zero
select replace(0.00234690616839645663803848176618444236941,0) from dual;
then I try to do the round function on it , but its giving me zero any idea how can we get this
select round(replace(0.00234690616839645663803848176618444236941,0) ) from dual;
You can try this :
select round(0.00234690616839645663803848176618444236941 * 1000,1) from dual;
Result:
2.3
When you strip out the zeros, you are left with .23.....
Select round (.23, 0) will return 0, because you are telling the db to round to 0 decimal places.
If you multiply the result of your replace by 10, that will get you want you want. Not sure what you are doing makes any sense, but it works:
select round (10 * (replace(0.00234690616839645663803848176618444236941,0)),1) from dual;
SQL Fiddle
I am getting result as decimal in stored procedure. If I am getting result as 123.45,
I want to split it into 123 and 45. Can anybody help?
use SQL function FLOOR() for getting integer part
and subtract that from the original for the decimal part
You can also make use of ROUND instead of FLOOR.
See section C. Using ROUND to truncate for trucate, and then subtract that from the original.
Be aware that using FLOOR on negative numbers might not give you the required result.
Have a look at this example
DECLARE #Dec DECIMAL(12,8)
SET #Dec = -123.45
SELECT FLOOR(#DEc)
select round(#Dec, 0, 1)
try this;
DECLARE #result DECIMAL(8,2) = 123.45
SELECT CAST(round(#result,0) AS FLOAT)
SELECT REPLACE(#result % 1 ,'0.','')
OR
DECLARE #result decimal(8,2) = 123.45
select PARSENAME(#result, 2) AS LeftSideValue, PARSENAME(#result, 1) AS RightSideValue