dbTalk Databases Forums  

Arithmetic overflow error converting numeric to data type numeric

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Arithmetic overflow error converting numeric to data type numeric in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
JJ297
 
Posts: n/a

Default Arithmetic overflow error converting numeric to data type numeric - 08-31-2010 , 06:57 AM






I'm getting that on this stored procedure:

it appears to be on this line: N'CAST((1.0 * icnt / NULLIF(tcnt, 0)
* 100) AS decimal
(6,
1)) AS perc

What do I change it to so I can get rid of the error?

SET NOCOUNT ON
Drop Table iClaimsYrPivot

DECLARE @pivot_cols NVARCHAR(MAX);
SELECT @pivot_cols =
STUFF((SELECT ',MAX(CASE WHEN year_month = ''' + year_month +
''' THEN perc END) AS [' + year_month + ']' +
',RANK() OVER(ORDER BY MAX(CASE WHEN year_month = '''

+
year_month +
''' THEN perc END)) AS [' + year_month + ' rank]' +
',SUM(CASE WHEN year_month = ''' + year_month +
''' THEN icnt END) AS [' + year_month + ' icnt]' +
',SUM(CASE WHEN year_month = ''' + year_month +
''' THEN tcnt END) AS [' + year_month + ' tcnt]'
FROM (SELECT DISTINCT RIGHT(CONVERT(VARCHAR(9),
CAST(dowrdt AS DATETIME), 6), 6),
CONVERT(CHAR(7), CAST(dowrdt AS DATETIME),
126)
FROM iclaimsYear) AS T(year_month, sort)
ORDER BY sort
FOR XML PATH('')
), 1, 1, '');

DECLARE @pivot_query NVARCHAR(MAX);
SET @pivot_query =
N'SELECT sort, reg, ' + @pivot_cols +
N' ,region, area, dist, doc, rpt ' +
N' INTO iClaimsYrPivot ' +
N'FROM (SELECT sort, reg, region, area, dist, doc, rpt, icnt, tcnt,
dowrdt, ' +
N'RIGHT(CONVERT(VARCHAR(9), CAST(dowrdt AS DATETIME),
6),
6) AS year_month, ' +
N'CAST((1.0 * icnt / NULLIF(tcnt, 0) * 100) AS
decimal
(6,
1)) AS perc ' +
N'FROM iClaimsYear) AS F ' +
N'GROUP BY sort, reg, area, dist, doc, rpt, region ' +
N'ORDER BY sort;';


EXEC(@pivot_query);

Reply With Quote
  #2  
Old   
Lutz Uhlmann
 
Posts: n/a

Default Re: Arithmetic overflow error converting numeric to data type numeric - 08-31-2010 , 07:10 AM






Am 31.08.2010 13:57, schrieb JJ297:
Quote:
I'm getting that on this stored procedure:

it appears to be on this line: N'CAST((1.0 * icnt / NULLIF(tcnt, 0)
* 100) AS decimal
(6,
1)) AS perc

What do I change it to so I can get rid of the error?
Maybe tcnt is NULL so the term becomes icnt / 0 ???

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Arithmetic overflow error converting numeric to data type numeric - 08-31-2010 , 07:19 AM



JJ297 (nc297 (AT) yahoo (DOT) com) writes:
Quote:
I'm getting that on this stored procedure:

it appears to be on this line: N'CAST((1.0 * icnt / NULLIF(tcnt, 0)
* 100) AS decimal
(6,
1)) AS perc

What do I change it to so I can get rid of the error?

Change decimal to float to see what values you get, and see if there is
anything suspicious.

When you the decimal data type, you should always specify precision and
scale explicitly, and not rely on defaults.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #4  
Old   
JJ297
 
Posts: n/a

Default Re: Arithmetic overflow error converting numeric to data type numeric - 08-31-2010 , 07:54 AM



On Aug 31, 8:19*am, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
JJ297 (nc... (AT) yahoo (DOT) com) writes:
I'm getting that on this stored procedure:

it appears to be on this line: * *N'CAST((1.0 * icnt / NULLIF(tcnt,0)
* 100) AS decimal
(6,
1)) AS perc

What do I change it to so I can get rid of the error?

Change decimal to float to see what values you get, and see if there is
anything suspicious.

When you the decimal data type, you should always specify precision and
scale explicitly, and not rely on defaults.

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Thanks I changed it to decimal(18,1) and that seemed to work. What do
you mean by this...

When you the decimal data type, you should always specify precision
and
scale explicitly, and not rely on defaults.

How would I do this?

Reply With Quote
  #5  
Old   
Lutz Uhlmann
 
Posts: n/a

Default Re: Arithmetic overflow error converting numeric to data type numeric - 08-31-2010 , 08:21 AM



Quote:
Thanks I changed it to decimal(18,1) and that seemed to work. What do
you mean by this...

When you the decimal data type, you should always specify precision
and
scale explicitly, and not rely on defaults.
You already do this with (18,1)!
I think he did not read this in your first post because of

Quote:
* 100) AS decimal
(6,
1)) AS perc
Lutz

Reply With Quote
  #6  
Old   
JJ297
 
Posts: n/a

Default Re: Arithmetic overflow error converting numeric to data type numeric - 08-31-2010 , 08:22 AM



On Aug 31, 9:21*am, Lutz Uhlmann <n... (AT) invalid (DOT) invalid> wrote:
Quote:
Thanks I changed it to decimal(18,1) and that seemed to work. *What do
you mean by this...

When you the decimal data type, you should always specify precision
and
scale explicitly, and not rely on defaults.

You already do this with (18,1)!
I think he did not read this in your first post because of

*>> * 100) AS decimal
*>> (6,
*>> 1)) AS perc

Lutz
Okay thanks!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.