Error converting data type varchar to numeric.

Trying to update a field from varchar.
I get the following
Error converting data type varchar to numeric.

What's wrong with this?     QUANITY -  HAS VARCHAR VALUES LIKE   100.00, 99 , -99.99
QUANTITY2 IS DECIMAL(18,9)

UPDATE ACCT SET QUANTITY2 = CAST(QUANTITY AS DECIMAL(18,9)) WHERE ISNUMERIC(QUANTITY) = 1
LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
One of the values in QUANTITY is not convertable to numeric, either because it has a non-number, thousands separator, empty string, etc..
Declare @foo varchar(50) 

-- These work
SET @foo = '-99.99'
SELECT CAST(@foo as DECIMAL(18,9))

SET @foo = '100.00'
SELECT CAST(@foo as DECIMAL(18,9))

SET @foo = NULL
SELECT CAST(@foo as DECIMAL(18,9))

-- These don't work
SET @foo = ''
SELECT CAST(@foo as DECIMAL(18,9))

SET @foo = '3.45+E09'
SELECT CAST(@foo as DECIMAL(18,9))

SET @foo = '1,234.56'
SELECT CAST(@foo as DECIMAL(18,9))

SET @foo = 'banana'
SELECT CAST(@foo as DECIMAL(18,9))

Open in new window

0
JElsterAuthor Commented:
I don't see the bad record .. Isnumeric returns 0
I have millions of records. Any ideas how I can find the bad 1?
0
Brendt HessSenior DBACommented:
Unfortunately, IsNumeric is not the most reliable function, since strings such as $1,234.56 returns a 1, as would a scientific notation number (1e44), a European format number (1.234,56), and a number of other variations that may not be handled correctly in your system.

The most reliable checks are manual, based on LIKE and counting legal characters such as the decimal point and minus sign to ensure that there are not too many in the field.  A layered approach will ensure that you don't do too much work.

The code below will identify all of the records where isNumeric = 1, then validate the values against additional checks, and will only update items that match the restrictions for a number.  Note that the code assumes a unique field, AccountID, for each ACCT record - change this to match your table.

;WITH NumRecs AS (
	SELECT AccountID
	FROM ACCT
	WHERE ISNUMERIC(QUANTITY) = 1
	)
UPDATE ACCT
SET QUANTITY2 = CAST(QUANTITY AS decimal(18,9))
FROM ACCT AS a
INNER JOIN NumRecs AS n
	ON a.AccountID = n.AccountID
WHERE QUANTITY NOT LIKE '%[^0-9.-]%'		-- has only numeric, decimal point, or minus sign characters in the string
	AND 
		CASE 
			WHEN LEN(QUANTITY) - LEN(REPLACE(QUANTITY, '.', ''))<=1	-- max of one decimal point
			AND LEN(QUANTITY) - LEN(REPLACE(QUANTITY, '-', '')) <=1		-- max of one minus sign
			AND CHARINDEX('-', QUANTITY) <=1										-- Minus sign first
				THEN 1
			ELSE 0
		END = 1

Open in new window

0
Brendt HessSenior DBACommented:
To find the bad record(s), reverse the logical checks, like this:

;WITH NumRecs AS (
	SELECT AccountID
	FROM ACCT
	WHERE ISNUMERIC(QUANTITY) = 1
	)
SELECT *
FROM ACCT AS a
INNER JOIN NumRecs AS n
	ON a.AccountID = n.AccountID
WHERE NOT (QUANTITY NOT LIKE '%[^0-9.-]%')		-- has only numeric, decimal point, or minus sign characters in the string
	OR 
		CASE 
			WHEN LEN(QUANTITY) - LEN(REPLACE(QUANTITY, '.', ''))<=1	-- max of one decimal point
			AND LEN(QUANTITY) - LEN(REPLACE(QUANTITY, '-', '')) <=1		-- max of one minus sign
			AND CHARINDEX('-', QUANTITY) <=1										-- Minus sign first
				THEN 1
			ELSE 0
		END = 0

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.