0

I need the output with 2 digit like 2.39 but I got the output 2.3980815347721822541966.

That is the problem I don't understand.

Here is my Query.

SELECT M.Description,A.Target,A.Actual,
  CASE
    WHEN A.Target > 0 THEN  A.Actual / CONVERT(DECIMAL(18,2),A.Target) 
    ELSE 0
  END Achievement
FROM (
  SELECT * FROM ProcessData PD 
  WHERE
    ProcessYear = 2015 AND ProcessMonth = 1
    AND DepotID = 6 AND ModuleID =1
) A 
LEFT OUTER JOIN Measure M
  ON M.ID = A.MeasureID
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Which RDBMS and what is the data type of A.Actual? What you have found in the RDBMS' documentation about data type conversion and arithmetic logic? Next time please show some effort and format your question to help us to help you! – Pred Jun 14 '16 at 08:35
  • 5
    Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Matt Gibson Jun 14 '16 at 08:36
  • Which type of converting i need to make the output in two digit? – user3510330 Jun 14 '16 at 08:38
  • There are different Rounding functions for the different environments. Witch Data-Base (RDBMS) are you using? BTW, I would say it is rounding you need, not conversion. – marlan Jun 14 '16 at 08:42

1 Answers1

2

You need to format the whole expression rather than just the divisor:

So rather than

THEN  A.Actual / CONVERT(DECIMAL(18,2),A.Target) 

Change it to:

THEN  CONVERT(DECIMAL(18,2), A.Actual / A.Target) 

Note that I would expect 2.398 to round to 2.40 (which is what the code I posted will do) - are you sure you want to round down always? If so, look at the ROUND function as well.

THEN  CONVERT(DECIMAL(18,2), ROUND(A.Actual / A.Target, 2, 1)
RB.
  • 36,301
  • 12
  • 91
  • 131