Conversion of SQL Server types

Asked

Viewed 54 times

1

I got the following SELECT in SQL Server:

SELECT  
    th.CidadeCod,
    th.ContratoCod,
    CONVERT(DECIMAL(5,2), ( SUM(th.PlacasLidas)/SUM(th.Trafego) ) * 100) AS [IndiceOCR]
FROM 
    controle.TrafegoFaixaHora th 
INNER JOIN ssis.Contratos c ON th.ContratoCod = c.ContratoCod AND th.CidadeCod = c.CidadeCod
INNER JOIN controle.Equipamentos e ON th.ContratoCod = e.ContratoCod AND th.CidadeCod = e.CidadeCod
WHERE 
    c.FlagAtivo = 1 AND e.FlagOcr = 1
GROUP BY 
    th.CidadeCod,
    th.ContratoCod

But the error is returned:

Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type int.

What should I do ?

  • is returning a value greater than 5 (including the whole part and decimal digits) in the calculation you are trying to convert. See the result of ( SUM(th.PlacasLidas)/SUM(th.Trafego) ) * 100. Return it without the convert to see the value size, hence you adjust the size of the decimal correctly

  • the Placaslides and Trafego fields are of what type?

2 answers

1


I decided as follows:

SELECT  
        th.CidadeCod
        ,th.ContratoCod
        ,SUM(CONVERT(BIGINT,th.Trafego)) AS [trafego]
        ,SUM(CONVERT(BIGINT,th.PlacasLidas)) AS [placaslidas]
        ,( 
        CONVERT(DECIMAL(15,2), 
        CONVERT(DECIMAL(15,5), SUM(CONVERT(BIGINT,th.PlacasLidas))) / 
        CONVERT(DECIMAL(15,5), SUM(CONVERT(BIGINT,th.Trafego))) 
        *100 )) AS [IndiceOCR]
FROM controle.TrafegoData th 
INNER JOIN ssis.Contratos c ON th.ContratoCod = c.ContratoCod AND th.CidadeCod = c.CidadeCod
INNER JOIN controle.Equipamentos e ON th.ContratoCod = e.ContratoCod AND th.CidadeCod = e.CidadeCod

WHERE 
    th.Data BETWEEN DATEADD(DAY,-14,GETDATE()) AND DATEADD(DAY,-8,GETDATE())
    AND c.FlagAtivo = 1 
    AND e.FlagOcr = 1

0

Alter

 CONVERT(DECIMAL(5,2), ( SUM(th.PlacasLidas)/SUM(th.Trafego) ) * 100) AS [IndiceOCR]

for something like this:

CONVERT(DECIMAL(15,2), ( SUM(th.PlacasLidas)/SUM(th.Trafego) ) * 100) AS [IndiceOCR]
  • didn’t work out that way

  • Still gave the same mistake?

  • yes. same mistake

  • Change to DECIMAL( 15, 5 ) . for example.

  • Same error. I put DECIMAL (15,15) and also was not

  • What numbers come in SUM( th.Placaslida ) and in SUM( th. Traffic ) ?

  • th. Trafego = 4244244928 e th.Placaslidas = 873313340. I have made the division as follows: ,( SUM(CONVERT(BIGINT,th.Placaslida)) / SUM(CONVERT(BIGINT,th.Trafego)) * 100 AS [Indiceocr] but 0 has been returned

  • I did with DECIMAL( 15, 10 ) with these numbers and it worked. Returned: 485.9933695700

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.