| vavsStarting Member
 
 
                                        24 Posts | 
                                            
                                            |  Posted - 2011-03-06 : 17:19:43 
 |  
                                            | I am having a problem on a stored procedure that has worked famously for months.  I was getting an error converting varchar to numeric (8114) I solved this by using the trim function to remove a trailing space on a string.  Now I am getting a new error, converting varchar to integer.  My problem is that I don't declare the field I am updating as an integer, I CAST it as a decimal.  Here is the stored procedure that I use to create the base table:code/CREATE procedure usp_tmiinva2asSELECT	TAG# 	, dbo.FRINFM.GRADE	, Cast(Gauge as varchar) as Gauge	, Width	, Weight	, CAST(Weight/2000.00 AS Decimal(6,2)) AS Tons	, Status	, CAST([Cost-Cwt] AS DECIMAL(6,2)) AS [Cost-Cwt]	, dbo.tblGrades.Type AS GaugeType	, SPACE(2) AS TYPE	, CAST(0.00 AS decimal(6,4)) AS DecIn	, CAST(0.00 AS decimal(6,4)) AS DecOut	, CAST(0.00 AS decimal(6,4)) AS DecAvg	, SPACE(8) AS GaugeFinal	, CAST(Weight*[Cost-Cwt]/100 AS MONEY) AS InvValue INTO dbo.TMIINVaFROM dbo.FRINFM	JOIN dbo.tblGrades ON dbo.FRINFM.GRADE = dbo.tblGrades.GradeWHERE	[status] IN (3, 4, 5, 6)union allSELECT	TAG# 	, dbo.FRINHISTFM.GRADE	, Cast(Gauge as varchar) as Gauge	, Width	, Weight	, CAST(Weight/2000.00 AS Decimal(6,2)) AS Tons	, Status	, CAST([Cost-Cwt] AS DECIMAL(6,2)) AS [Cost-Cwt]	, dbo.tblGrades.Type AS GaugeType	, SPACE(2) AS TYPE	, CAST(0.00 AS decimal(6,4)) AS DecIn	, CAST(0.00 AS decimal(6,4)) AS DecOut	, CAST(0.00 AS decimal(6,4)) AS DecAvg	, SPACE(6) AS GaugeFinal	, CAST(Weight*[Cost-Cwt]/100 AS MONEY) AS InvValue from dbo.FRINHISTFMJOIN dbo.tblGrades ON dbo.FRINHISTFM.GRADE = dbo.tblGrades.GradeWHERE	[status] IN (3, 4, 5, 6)GOcode/What I am doing from this point is taking a field called Gauge and parsing it so that I can separate a string like .121/.122 into two fields DecIn and DecOut.  I use the mid function to find the slash then return what is left or right.  The code is here:code/CREATE procedure usp_tmiinva4asupdate dbo.tmiinva set DecIn = 	CASE	WHEN CHARINDEX('/',Gauge)>0 THEN SUBSTRING(Gauge,1,(charindex('/',Gauge)-1))	WHEN CHARINDEX('-',Gauge)>0 THEN SUBSTRING(Gauge,1,(CHARINDEX('-',Gauge)-1))	WHEN CHARINDEX('MIN',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('MIN',Gauge)-1)))	WHEN CHARINDEX('MAX',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('MAX',Gauge)-1)))	WHEN CHARINDEX('NOM',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('NOM',Gauge)-1)))	WHEN CHARINDEX('ACT',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('ACT',Gauge)-1)))	else 0endupdate dbo.tmiinvaset DecOut = 	CASE	WHEN CHARINDEX('/',Gauge)>0 THEN SUBSTRING(Gauge,(charindex('/',Gauge)+1),99)	WHEN CHARINDEX('-',Gauge)>0 THEN SUBSTRING(Gauge,(charindex('-',Gauge)+1),99)	WHEN CHARINDEX('MIN',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('MIN',Gauge)-1)))	WHEN CHARINDEX('MAX',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('MAX',Gauge)-1)))	WHEN CHARINDEX('NOM',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('NOM',Gauge)-1)))	WHEN CHARINDEX('ACT',Gauge)>0 THEN RTRIM(SUBSTRING(Gauge,1,(CHARINDEX('ACT',Gauge)-1)))	else 0endGOcode/I added the RTRIM to the substring to get rid of a situation where the Gauge field contains .121 NOM.  That would return .121space which would cause an error.  I am really struggling on this one since it has been working and I don't know of anything that has been added. |  |