Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do we get decimal 2 places without rounding in sql
for simple calculation,
(3990-460)/95 = 35.2 but sql give 36.
how do i do it?

What I have tried:

tried round, floor. it not works
Posted
Updated 5-Feb-20 20:11pm
Comments
Herman<T>.Instance 23-Jan-20 5:05am    
Which type of database?

Quote:
(3990-460)/95 = 35.2
Please note, the result of (3990-460)/95 is 37,15789...

Anyway, depending on your actual SQL platform, might be a TRUNCATE or TRUNC function is available (see, for instance Oracle's TRUNC (number)[^]).
 
Share this answer
 
It's almost certainly a integer / conversion problem: if I use DECIMAL values throughout, I get what I expect.
SQL
DECLARE @A DECIMAL(7,2) = 3990
DECLARE @B DECIMAL(7,2) = 460
DECLARE @C DECIMAL(7,2) = 95
DECLARE @VAL DECIMAL(7,2) = (@A - @B) / @C

DECLARE @IA INTEGER = 3990
DECLARE @IB INTEGER = 460
DECLARE @IC INTEGER = 95
DECLARE @IVAL DECIMAL(7,2) = (@IA - @IB) / @IC
SELECT (@A - @B) /  @C, (@IA - @IB) /  @IC, @VAL, @IVAL, (3990 - 460) / 95

(No column name)  (No column name)  (No column name)  (No column name) 
 (No column name)
37.1578947368     37                37.16             37.00             37

So check your value types! At a guess, you are using the wrong column type, but we can't check your DB - we don;t have any access to it.
 
Share this answer
 
If you use SQL Server.
SQL
SELECT FORMAT((3990-460)/CAST(95 as Decimal), 'N2');
 
Share this answer
 
v2
Comments
Jörgen Andersson 23-Jan-20 9:20am    
I wouldn't use FORMAT as it changes the type to a string.
Better use Round((3990-460)/CAST(95 as Decimal),2,1) or CAST((3990-460)/CAST(95 as Decimal) AS Decimal(7,2))
Thanks all. one thing to take note for those facing issue like me is your DB table data type and SP data type must be matched. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900