Is there a equivalent roundup of excel to sql query? - sql

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

Related

Round-up or Round-down the SpecialOfferPrice column in this query?

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

SQL Cast to show decimals

I have the following statement within my select clause;
(([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as Total_Completed
How do I implement CAST "cast(your_float_column as decimal(10,2))" ? I want my column Total_Completed to show 2 decimal places
I cannot seem to get the correct syntax!
Thank you
the result of the calculation depends of the used columns type.
If you divide int columns, you get int result : 1 / 6 = 0
when you convert each values to decimal the result is: 1 / 6 = 0.1666666666666
Now you want 2 decimal result,so you have to convert/ round the previous result to get the expected value
See fiddle for some example of divide and cast / round : http://sqlfiddle.com/#!18/51785/5
An easy trick can be to use :
round ( 1.0 * ( [Complt_Emp] + [No_Non_Complt_Emp] ) / [No_of_Emp] , 2 )
Cast each expression seperately
CAST(([Complt_Emp] + [No_Non_Complt_Emp]) as decimal(10,2)) /
CAST([No_of_Emp] as decimal(10,2)) as Total_Completed
I suspect all your values are INT. An int divided by an int will return an int.
(([Complt_Emp] + [No_Non_Complt_Emp])/ cast([No_of_Emp] as decimal(10,2)) as Total_Completed
Try this
cast((([Complt_Emp] + [No_Non_Complt_Emp])/ [No_of_Emp]) as decimal(10,2)) as Total_Completed

CEILING in SQL does not return correct values

CEILING in T-SQL does not return correct values in SQL Server 2012. Please see below details.
select CEILING(0.3333333333333333)
Result: 1
But I want to do a calculation of based on count of columns. Internal calculation is given below.
select CEILING((17 - 2) / 45)
Result: 0
(17 - 2) / 45 = 0.33333
But
select CEILING((17 - 2) / 45)
returns zero
I want to get one as result. What should I do?
It ended with a integer division. So
15/45 = 0
Try CASTing atleast one of the operand into decimal ,
SELECT CEILING((17-2)/CAST(45 AS DECIMAL (4,2))
OR like,
SELECT CEILING(15,45.0)

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;
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;

Splitting decimal in sql

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