Arithmetic overflow error converting expression to data type float

Hello All,
  • Remove From My Forums
  • Question

  • Hello All,

    My stored proc is failing because of the error Arithmetic overflow error converting expression to data type float.

    I can see the number causing the problem is -1.79769313486232E+308. I have tried to convert it with other date types but nothing helps. I am in hurry as we have a release soon and can’t afford to waste time.

    Please suggest the data type I should be using.

    Thanks,

    Pragati


    Best Regards, Pragati

Answers

    • Marked as answer by

      Wednesday, October 3, 2018 2:57 PM

  • If you have a range that you can rely I would suggest to use Case or even better a where clause:

    Your Piece of code

    where your_field > -100000………. so you can wipe out the bad number. 

    • Marked as answer by
      Pragati Sharma
      Wednesday, October 3, 2018 2:57 PM

This blog describes the fixing of the error below:

Date and Time:

6-10-2011 10:25:45

Log Name:

Operations Manager

Source:

Health Service Modules

Event Number:

31552

Level:

1

Logging Computer:

OPSRMS01

User:

N/A

Description:

Failed to store data in the Data Warehouse. Exception ‘SqlException’: Sql execution failed. Error 777971002, Level 16, State 1, Procedure StandardDatasetAggregate, Line 424, Message: Sql execution failed. Error 777971002, Level 16, State 1, Procedure PerformanceAggregate, Line 149, Message: Sql execution failed. Error 8115, Level 16, State 2, Procedure -, Line 1, Message: Arithmetic overflow error converting expression to data type float. One or more workflows were affected by this. Workflow name: Microsoft.SystemCenter.DataWarehouse.StandardDataSetMaintenance Instance name: Performance data set Instance ID: {7547DA11-6328-54C6-00D6-C0729CD41CD8} Management group: SCOM01

Research:

It seems the aggregation of the hourly performance tables wend wrong. But what table are we talking about?

Okay looking at the error message the stored procedure what caused the error is PerformanceAggregate . Looking at this procedure you will see the SQL code that is giving the problem below.

SET @Statement =
        'INSERT ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@InsertTableName) + ' ('
      + '  [DateTime]'
      + ' ,PerformanceRuleInstanceRowId'
      + ' ,ManagedEntityRowId'
      + ' ,SampleCount'
      + ' ,AverageValue'
      + ' ,MinValue'
      + ' ,MaxValue'
      + ' ,StandardDeviation'
      + ')'
      + ' SELECT'
      + '    CONVERT(datetime, ''' + CONVERT(varchar(50), @IntervalStartDateTime, 120) + ''', 120)'
      + '   ,PerformanceRuleInstanceRowId'
      + '   ,ManagedEntityRowId'
      + '   ,COUNT(*)'
      + '   ,AVG(SampleValue)'
      + '   ,MIN(SampleValue)'
      + '   ,MAX(SampleValue)'
      + '   ,ISNULL(STDEV(SampleValue), 0)'
      + ' FROM ' + QUOTENAME(@SchemaName) + '.' + QUOTENAME(@CoverViewName)
      + ' WHERE ([DateTime] >= CONVERT(datetime, ''' + CONVERT(varchar(50), @IntervalStartDateTime, 120) + ''', 120))'
      + '   AND ([DateTime] < CONVERT(datetime, ''' + CONVERT(varchar(50), @IntervalEndDateTime, 120) + ''', 120))'
      + ' GROUP BY PerformanceRuleInstanceRowId, ManagedEntityRowId'

Since we are investigating a performance issue the @SchemaName and @CoverViewName would be ‘Perf.vPerfRaw’. Now we have to determine the correct values for the @IntervalStartDateTime and @IntervalEndDateTime. This can be done by looking at the StandardDatasetAggregationHistory table, by running the query below. We know it’s a performance issue so we look at the performance aggregate dataset and then we look in the history table for the last good aggregation for this dataset.

declare @DataSetId as uniqueidentifier

select top 1 @DataSetId=SDS.DataSetId from dbo.StandardDatasetAggregation SDA

inner join StandardDataSet SDS on SDS.DataSetId=SDA.DataSetId

where SDA.BuildAggregationStoredprocedureName like '%PerformanceAggregate%'

select * from dbo.StandardDatasetAggregationHistory SDA

inner join dbo.StandardDataset SD on SD.DatasetId=SDA.DatasetId

where DirtyInd=1 and SDA.DataSetId=@DataSetId

order by AggregationDateTime ASC

And whala the fist record below gives me the data period caused my error:

clip_image001

So I change the @IntervalStartDateTime = 2011-09-28 22:00:00  and @IntervalEndDateTime = 2011-09-30 04:01:28. And the query to execute is born:

SELECT CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 22:00:00', 120), 120) 

,PerformanceRuleInstanceRowId 

,ManagedEntityRowId 

,COUNT(*) 

,AVG(SampleValue) 

,MIN(SampleValue) 

,MAX(SampleValue) 

,ISNULL(STDEV(SampleValue), 0) 

FROM Perf.vPerfRaw 

WHERE ([DateTime] >= CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 21:00:00', 120), 120)) 

AND ([DateTime] < CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 04:01:00', 120), 120)) 

GROUP BY PerformanceRuleInstanceRowId, ManagedEntityRowId 

Hmm but this query’s gives me:

clip_image002

Yes this is exactly what we want. Now we are going to to change the end date to a lower period so we can isolate the record giving the overflow. Doing this I am getting the error period is ‘2011-09-29 21:05:45’

So next is to hunt down this bad record:

SELECT PerformanceRuleInstanceRowId 

,ManagedEntityRowId 

,SampleValue 

FROM Perf.vPerfRaw 

WHERE ([DateTime] = CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 21:05:45', 120), 120)) 

order by SampleValue 


Wooaaw found it:

clip_image003

Hmm the STDEV doesn’t like showing large negative number.

Let’s look what this function does:

Returns the statistical standard deviation of all values in the specified expression. May be followed by the OVER clause.

Okay we could look and investigate what data value we must change it to but I am not willing to spend to much time. Since the value is soooo large I probably assume the measurement was false. So I will change it to 0.

I you wanted still to investigate you could use the query below and change the E+217 to a lower value till the query runs okay:

declare @float as Float 

set @float = -1.1031304526204E+217 

select @float 

select STDEV(@float) 


p.s E+154 is the maximum you can apply ;-))

As i said I am going to change this bad records to 0. Since we are looking at a view and this view isn’t updatable we have first to find out the root table containing this data. This isn’t so hard.

The query below gives you the performance RAW table containing the records:

The dadasetid is the same as you had got back in the first query as @DataSetId.

SELECT [StandardDatasetTableMapRowId] 

,[DatasetId] 

,[AggregationTypeId] 

,[TableGuid] 

,[TableNameSuffix] 

,[InsertInd] 

,[OptimizedInd] 

,[StartDateTime] 

,[EndDateTime] 

FROM [OperationsManagerDW].[dbo].[StandardDatasetTableMap] 

where datasetid = '1B1F0F44-A208-4145-8E59-9121357D78F2' 

and [AggregationTypeId] = 0 

and '2011-09-29 21:05:45' between [StartDateTime] and [EndDateTime] 


Running this query will give you below the table we have to change:

clip_image004

Yes yes finally we are there. Now we are going to update the records. The table to use is : Perf.PerfRaw_E721608C35A44620AE3E0DE028C3C5A2

So the update query is:

update Perf.PerfRaw_E721608C35A44620AE3E0DE028C3C5A2 

set SampleValue = 0 

WHERE ([DateTime] = CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 21:05:45', 120), 120)) 

and SampleValue = -1.1031304526204E+217 


The result is , as expected:

clip_image005

Lets check if its now fixed:

SELECT CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 22:00:00', 120), 120) 

,PerformanceRuleInstanceRowId 

,ManagedEntityRowId 

,COUNT(*) 

,AVG(SampleValue) 

,MIN(SampleValue) 

,MAX(SampleValue) 

,ISNULL(STDEV(SampleValue), 0) 

FROM Perf.vPerfRaw 

WHERE ([DateTime] >= CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 21:05:45', 120), 120)) 

AND ([DateTime] < CONVERT(datetime, + CONVERT(varchar(50), '2011-09-29 21:07:50', 120), 120)) 

GROUP BY PerformanceRuleInstanceRowId, ManagedEntityRowId 


Gives me back:

clip_image006

SO IT’S FIXED !!!

But what targets workflows caused this bad data. Take the ManagedEntityRowId and PerformanceRuleInstanceRowId data from the bad records.

Below the query for the guilty targets:

select * from dbo.ManagedEntity 

where ManagedEntityRowId in (103425,103424,103426) 


clip_image007

And the below the query for the related workflows:

SELECT PerformanceRule.ObjectName, PerformanceRule.CounterName, PerformanceRuleInstance.InstanceName 

FROM PerformanceRule INNER JOIN 

PerformanceRuleInstance ON PerformanceRule.RuleRowId = PerformanceRuleInstance.RuleRowId 

WHERE (PerformanceRuleInstance.PerformanceRuleInstanceRowId = 346638) 


clip_image008

Happy SCOMMING!

Michel Kamp

This error comes when you try to store an out-of-range floating point value into a numeric variable. For example, if your NUMERIC or DECIMAL variable is defined as NUMERIC(5,2) then the maximum value it can hold is 999.99, if you try to store something like 1000.00 then it will throw «Arithmetic overflow error converting numeric to data type numeric». One of the common reasons for this error is the ignorance and misunderstanding of the NUMERIC data type. For example, many SQL Server DBAs and developers think that a NUMERIC(5,2) variable can hold a 7 digit floating point number where 5 digits are before the decimal and 2 digits are after the decimal. This is wrong.

A NUMERIC(5,2) means, the total number of digits in the value cannot exceed 5 and decimal precision is 2 digits i.e. the maximum possible value is 999.99.

Another thing SQL programmers don’t know and remember about NUMERIC or DECIMAL data types in Microsoft SQL Server is that it doesn’t throw this error if you specify more digits than permitted after the decimal point, instead, it does rounding for example if you store 100.999 then it will store 101.00 after rounding.

Here is an SQL query to prove these points:

DECLARE @sample NUMERIC(5,2)
SET @sample = 1000.554
SELECT @sample

Output
Arithmetic overflow error converting numeric to data type numeric.

Explanation: 

This time SQL Server throws the error because we are trying to store 1000 but the maximum value a NUMERIC(5,2) can hold is 999 before the decimal point. You need to increase the width of the variable to store this number e.g. making @sample NUMERIC(6,2) will solve this error as shown below.

By the way, if you are new to Microsoft SQL Server and T-SQL then I also suggest you join a comprehensive course to learn SQL Server fundamentals and how to work with T-SQL. If you need a recommendation then I suggest you go through the Microsoft SQL for Beginners online course by Brewster Knowlton on Udemy. It’s a great course to start with T-SQL and SQL queries in SQL Server.

Arithmetic overflow error converting numeric to data type numeric in SQL Server

Arithmetic overflow error converting numeric to data type numeric in SQL Server Examples

Here are some more SQL queries which will confirm the behavior of the NUMERIC variable and its range:

DECLARE @sample NUMERIC(5,2)
SET @sample = 100.554 // no rounding because the extra digit is less than 5
SELECT @sample AS Result

Result
100.55

DECLARE @sample NUMERIC(5,2)
SET @sample = 100.555 // rounding will happen
SELECT @sample AS Result

Result
100.56


DECLARE @sample NUMERIC(5,2)
SET @sample = 100.55 // no rounding because value is under defined precision
SELECT @sample AS Result

Result
100.55


DECLARE @sample NUMERIC(5,2)
SET @sample = 100.999 // Rounding to nearest value
SELECT @sample AS Result

Result
101.00

DECLARE @sample NUMERIC(5,2)
SET @sample = 999.999 // error because after rounding value will be
                      // out-of-range for defined numeric type
SELECT @sample AS Result

Result
Arithmetic overflow error converting numeric to data type numeric.

That’s all about «Arithmetic overflow error converting numeric to data type numeric in SQL Server». You can see that cause of the error is usually out-of-range value for the defined NUMERIC type. Just check the source of value and correct or increase the precision level of your column.

Always remember that NUMERIC(5,2) means a total of 5 digits with 2 digits after the decimal point, and the maximum value it can hold is 999.99. Beware of rounding due to more additional digits after the decimal point, which can also cause «Arithmetic overflow error converting numeric to data type numeric» in Microsoft SQL Server.

Related SQL Server articles you may like to explore

  • Difference between rank(), row_number(), and dense_rank() in SQL? (answer)
  • How to replace NULL with empty String in SQL Server? (tutorial)
  • Difference between Cast, Convert, and Parse method in SQL? (answer)
  • Difference between coalesce() and isNull() in Microsoft SQL Server? (answer)
  • How to remove duplicate rows from a table in SQL? (solution)
  • How to create an Identity column in SQL Server? (example)
  • How to format Date in Microsoft SQL Server? (example)
  • 5 Web sites to learn SQL online for FREE? (resource)
  • How to find the length of a string in SQL Server? (example)
  • How to convert the result of a SELECT command into a CSV String? (example)
  • The right way to check for NULL values in the SQL query? (example)
  • How to split String in SQL Server? (answer)
  • What is the difference between close and deallocate a cursor? (answer)
  • How to find all customers who have never ordered? (solution)
  • The right way to compare dates in SQL query? (example)
  • How to add columns into an existing table in MSSQL? (example)

Thanks for reading this article. If you like the information given here and my explanation then please share it with your friends and colleagues. 

With SQL Server, how to avoid this error : “Arithmetic overflow error converting expression to data type int”. Let’s insert some data into a table using a stored procedure for example. Facing this SQL Server error message can be disturbing. Even if SQL conversion errors are classical ones, finding the solution and apply the industry best practices to avoid them is very useful.

In this case, the stored procedure executing the query generates some dynamic T-SQL code. Then it executes the code and insert data from various tables into one single target table. It’s a typical SQL Server int arithmetic overflow conversion error.

How to avoid the SQL Server Arithmetic overflow error?

While running the same piece of T-SQL code independently, that is outside the stored procedure, directly into the SQL Server Management Studio windows, no error is faced. But when integrating the same code into the stored procedure, this error is thrown:

Msg 50000, Level 16, State 2, Procedure MyStoredProcedure, Line 123
Arithmetic overflow error converting expression to data type int.

After analysis, the conversion error message didn’t come from the SELECT clause. The SQL INSERT clause from the same T-SQL stored procedure is throwing the error. The root cause is the the insertion of a big integer value into the table. For integer Data Type in SQL Server, the number must be between -2^31 (-2 to the power of 31) and 2^31 (2 to the power of 31).

Namely the range for the INTEGER type is exactly between -2,147,483,648 and 2,147,483,648.

Use another data type to avoid the conversion error

The solution to avoid Arithmetic overflow error converting expression is to use a bigger data type. The solution to avoid this arithmetic overflow error is to change the data type from INT to BIGINT or DECIMAL(11,0) for example.

Please note that in this case the conversion error is because the integer number is too big. It can also be an insertion of a text value into an integer field. Check out the int, bigint, smallintand tinyint official documentation for SQL Server.

Other classical conversion questions

This article shows how to simply avoid an Arithmetic overflow error converting expression to int in SQL Server. It happens when inserting a bigger number in an integer data type. Check out more classical SQL Server errors like the insert of a duplicate key.

What is a SQL Server arithmetic overflow error?

An arithmetic overflow error is an error faced when converting from one SQL Server data type to another one. For example from a numeric figure to an integer because the integer type can handle smaller figures compared to the numeric type.

What’s the biggest number for a SQL Server INTEGER?

The biggest number for the SQL Server integer data type is from -2 to the power of 31 till 2 to the power of 31. It means from -2^31 to 2^31 which is exactly this range: from -2,147,483,648 to 2,147,483,648. To avoid conversion error, always consider the maximum value for the data and size the database and the data types accordingly.

What’s the difference between INT and BIGINT?

A bigint is an integer, i.e. a number without decimals, and in SQL Server it can go from -2^63 that is exactly -9,223,372,036,854,775,808 to 2^63-1 which is 9,223,372,036,854,775,807 and is using 8 Bytes in storage.
While an INTEGER is from -2^31 which equals -2,147,483,648 to 2^31-1 which is exactly 2,147,483,647. An INTEGER data is stored on 4 Bytes.

How to avoid conversion errors with SQL Server?

One good practice to avoid conversion errors during is to put in place controls for the inputs. And moreover make sure to control the values inserted in the database. For example, users can only enter valid dates in a date field.

Check out how to manage time difference in hours minutes and also seconds with T-SQL.

Hi everyone, google hasn’t helped so here I am. I am having some issues with that error message coming up during an update. The update and further description follows. I am fairly certain the issue is with the [lot] columns as the [lot] in lotupdate$ is a float, where as it is an nvarchar in location inventory. I have tried adding a column to the lotupdate$ table as nvarchar and setting lot=to it, but the entry changes from 12345678 to 1.23456e+006 and I have no idea why. when I try to cast lot as nvarchar I get the following error message :

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

Anyone have any ideas?

update [ils].[dbo].[LOCATION_INVENTORY]

set [ils].[dbo].[LOCATION_INVENTORY].[on_hand_qty]= [dbo].[lotupdate$].[on_hand_qty]

from [ils].[dbo].[LOCATION_INVENTORY] inner join [ils].[dbo].[lotupdate$] on [ils].[dbo].[LOCATION_INVENTORY].[ITEM] = [ils]. [dbo].[lotupdate$].[ITEM]

where [ils].[dbo].[LOCATION_INVENTORY].[LOCATION] = [ils].[dbo].[lotupdate$].[LOCATION]

and [ils].[dbo].[LOCATION_INVENTORY].[lot] = [ils].[dbo].[lotupdate$].[lot]

Понравилась статья? Поделить с друзьями:
  • Arithmetic error floating point overflow signalled
  • Ariston холодильник как изменить температуру
  • Ariston ошибка f07
  • Ariston ошибка e24
  • Ariston ошибка 5p3