sentil kumar
sentil kumar

Reputation: 95

SQL decimal conversion

I have some problem during converting this decimal value shown below:

21.6080402010050

Can anyone help me to convert this to become 21.6?

I have tried with this but it doesn't work.

(convert(decimal(10, 2), SUM(t1.TOTAL))

This is the query

SELECT 
    t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, 
    t1.FAMILY, t1.CACHE_FAMILY, 
    SUM(t1.TOTAL) AS CountOfLineNo, t2.TotalOfLineNo, 
    (convert(decimal(8,1), SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo ) AS Percentage
FROM (
    SELECT 
        LINE_NO, E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY, 
        Count(LINE_NO) as Total
    FROM TX_HSA_SUMM
    WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
    GROUP BY LINE_NO, E_FIELD, F_FIELD, G_FIELD, HSA_STATUS, FAMILY, CACHE_FAMILY) AS t1 
LEFT JOIN (
      SELECT LINE_NO, COUNT(LINE_NO) AS TotalOfLineNo
      FROM TX_HSA_SUMM
      WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
      GROUP BY LINE_NO) AS t2 ON t1.LINE_NO = t2.LINE_NO
GROUP BY 
   t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, 
   t1.CACHE_FAMILY, t1.Total, t2.TotalOfLineNo
ORDER BY 
   t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, 
   t1.CACHE_FAMILY, t1.Total, t2.TotalOfLineNo

and output is

LINE_NO E_FIELD F_FIELD G_FIELD HSA_STATUS FAMILY CACHE_FAMILY CountOfLineNo    TotalOfLineNo   Percentage
23053B  00000   00000   00000   S   SUMMIT  WER 43  199 21.608040201005%

Upvotes: 0

Views: 8709

Answers (4)

Naresh Pansuriya
Naresh Pansuriya

Reputation: 2045

Please try with below query

SELECT t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, t1.CACHE_FAMILY, 
    SUM(t1.TOTAL) AS CountOfLineNo, t2.TotalOfLineNo, convert(decimal(18,1),(convert(decimal(8,1),SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo)) AS Percentage
    FROM (
          SELECT LINE_NO, E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY, Count(LINE_NO) as Total
          FROM TX_HSA_SUMM
          WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
          GROUP BY LINE_NO,E_FIELD,F_FIELD,G_FIELD,HSA_STATUS,FAMILY,CACHE_FAMILY
        ) AS t1 
    LEFT JOIN (
          SELECT LINE_NO, COUNT(LINE_NO) AS TotalOfLineNo
          FROM TX_HSA_SUMM
          WHERE MT_TIMESTAMP2 BETWEEN ('2013-03-07 10:10:00') AND ('2013-03-08 10:20:00')
          GROUP BY LINE_NO
          ) AS t2 
      ON t1.LINE_NO = t2.LINE_NO
    GROUP BY t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, t1.CACHE_FAMILY, t1.Total,t2.TotalOfLineNo
    ORDER BY t1.LINE_NO, t1.E_FIELD, t1.F_FIELD, t1.G_FIELD, t1.HSA_STATUS, t1.FAMILY, t1.CACHE_FAMILY, t1.Total,t2.TotalOfLineNo

Upvotes: 1

sgeddes
sgeddes

Reputation: 62831

This should work for you:

SELECT ROUND(21.6080402010050,1)

SQL Fiddle Demo

It should work with most RDBMS.

With your edits:

ROUND((convert(decimal(8,1),SUM(t1.TOTAL)) * 100 / t2.TotalOfLineNo ),1)

Upvotes: 0

Jeremy Wiggins
Jeremy Wiggins

Reputation: 7299

Have a look at the ROUND function.

select round(21.6080402010050, 1)

Will work on MS SQL Server or MySQL

Upvotes: 0

Dhinakar
Dhinakar

Reputation: 4151

Use Round function

select round(21.6080402010050,1)

Upvotes: 0

Related Questions