Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Arithmetic overflow error

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-08-31 : 07:40:54
I'm getting Arithmetic overflow error converting numeric to data type numeric on this stored procedure while trying to bring in data on the fly.

It's this line: N'CAST((1.0 * icnt / NULLIF(tcnt, 0) * 100) AS decimal
(6,
1)) AS perc

I tried to change it to
N'CAST((1.0 * icnt / NULLIF(tcnt, 0) * 100) AS decimal
(7,
3)) AS perc but that didn't work.

Any ideas?


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);

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-08-31 : 08:06:32
Parentheses are missing


CAST(
( 1.0 * icnt / NULLIF(tcnt, 0) * 100
) AS decimal



SELECT CAST((1.0E * @icnt / ( @tcnt * 100)) AS decimal(6, 1))

Jim


Everyday I learn something that somebody else already knew
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-08-31 : 08:52:29
I tried it with decimal (18,1) and that worked.
Go to Top of Page
   

- Advertisement -