May 17, 2011 at 1:21 pm
I'm extracting numbers from a text field and I keep getting this:
Conversion failed when converting the nvarchar value '¹' to data type int.
And yet I can't find which row contains char(185) using LIKE, CHARINDEX, PATINDEX, or anything else using either CHAR(185) or the literal character copy/pasted.
ANYONE?
I'd like to replace the CHAR(185) with a 1 or anything else for that matter.. if I could just find those particular rows.
Donalith
May 17, 2011 at 1:38 pm
Couldn't you just use the ISNUMERIC() function on that field and anything with a zero won't cast to an int?
--------------------------------------------------------------------------
When you realize you've dug yourself into a hole....Step 1...stop digging.
May 17, 2011 at 1:49 pm
here's one way to do it; I'm just creating a table full of high ascii chars, cross applying them, then using patindex to filter:
with mySampleData
As
(SELECT top 255 row_number() OVER (ORDER by name) as N FROM sys.columns),
MyHighAscii
AS
(SELECT CHAR(N) As TheChar FROM mySampleData WHERE N >=127)
SELECT *
FROM (SELECT 'é' As TheData UNION ALL
SELECT 'happinéss ës a warm blankët' As TheData UNION ALL
SELECT 'Other~stuff' )MyData
cross Join MyHighAscii
WHERE PATINDEX('%' + MyHighAscii.TheChar + '%',TheData) > 0
Lowell
May 17, 2011 at 1:49 pm
ChazMan (5/17/2011)
Couldn't you just use the ISNUMERIC() function on that field and anything with a zero won't cast to an int?
Unfortunately no. CHAR(185) qualifies as a numeric in the ISNUMERIC() function.
I didn't want to do it but I'll give you the query I'm running.
selectSUBSTRING(SUBSTRING(answertext,PATINDEX('%[0-9]%',answertext), LEN(answertext)),0,patindex('%[^0-9]%',SUBSTRING(answertext,PATINDEX('%[0-9]%',answertext), LEN(answertext))))
, AnswerText
, QuestionName
, StateEnum
from Table_Name_Obfuscated
where ascii(SUBSTRING(SUBSTRING(answertext,PATINDEX('%[0-9]%',answertext), LEN(answertext)),0,patindex('%[^0-9]%',SUBSTRING(answertext,PATINDEX('%[0-9]%',answertext), LEN(answertext))))) between 48 and 57
and patindex('%[0-9]%',answertext)>0
May 17, 2011 at 1:58 pm
All fixed...thanks folks. I must have misused the PATINDEX the first time.
I updated my table with this:
update TABLE_NAME_OBFUSCATED set AnswerText = REPLACE(AnswerText, CHAR(185),1)
where PATINDEX('%' + char(185) + '%',AnswerText)>0
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply