mburk1968
asked on
How to replace special character in SQL 2005
I am attempting to extract data from an XML string contained in a SQL table and place it into individual fields in a SQL table. The problem that I am having is that the apostrophe contained in the XML string is from MS word and my data imports with errors. This error is mainly on the following fields QuestionText and AnswerText in the code below. Can anyone help me figure this out.
Thank You
Thank You
USE [EXPORTHpixPROD]
GO
/****** Object: StoredProcedure [dbo].[sp_CME_Parse_XML_Test] Script Date: 07/20/2009 07:35:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_CME_Parse_XML_Test]
AS
DECLARE @strXML varchar(max)
DECLARE @XMLID int
DECLARE @myXMLID int
DECLARE @TestId as int
DECLARE @getData CURSOR
SET @getData = CURSOR FOR
SELECT XMLID, xmlDATA
FROM CME_Test_Results_XML
WHERE Processed = 0
DECLARE @intPointer int
OPEN @getData
FETCH NEXT
FROM @getData INTO @myXMLID, @strXML
WHILE @@FETCH_STATUS = 0
BEGIN
exec sp_xml_preparedocument @intPointer output, @strXML
INSERT INTO CME_Parsed_Test_XML
SELECT TestDate,
Pass_Fail,
TestScore,
EventID,
LicenseNumber,
RegistrationTime,
SLUserID
FROM OPENXML(@intPointer, '/Test', 2)
WITH (
[TestDate][datetime] './TestDate',
[Pass_Fail][nvarchar](10) './Pass_Fail',
[TestScore][nvarchar](255) './TestScore',
[EventID][bigint] './EventID',
[LicenseNumber][nvarchar](50) './LicenseNumber',
[RegistrationTime][datetime] './RegistraionTime',
[SLUserID][bigint] './SLUserID'
)
SELECT @TestId = SCOPE_IDENTITY()
INSERT INTO CME_Parsed_Test_Results_XML
SELECT TestID = @TestId,
QuestionID,
QuestionText,
COALESCE(AnswerID, -1) as AnswerID,
replace(AnswerText,char(39), char(39)+char(39)),
''
FROM OPENXML(@intPointer, '/Test/QuestionID/AnswerText', 2)
WITH (
[AnswerText][nvarchar](4000) 'node()',
[QuestionID][int] '../@id',
[QuestionText][nvarchar](4000) '../QuestionText',
[AnswerID][int] './@id'
) WHERE AnswerID is not NULL
DECLARE @addtlText as nvarchar(4000)
DECLARE @addtlID as int
SELECT @addtlID = QuestionID,
@addtlText = AnswerText
FROM OPENXML(@intPointer, '/Test/QuestionID/AnswerText', 2)
WITH (
[AnswerText][nvarchar](4000) 'node()',
[QuestionID][int] '../@id',
[QuestionText][nvarchar](4000) '../QuestionText',
[AnswerID][int] './@id'
) WHERE AnswerID is NULL
IF (@addtlID > 0)
BEGIN
UPDATE CME_Parsed_Test_Results_XML SET AnswerText2 = replace(@addtlText,char(39), char(39)+char(39)) WHERE QuestionID = @addtlID AND TestID = @TestId
END
exec sp_xml_removedocument @intPointer
UPDATE CME_Test_Results_XML SET Processed = 1 WHERE XMLID = @myXMLID
FETCH NEXT
FROM @getData INTO @myXMLID, @strXML
END
CLOSE @getData
DEALLOCATE @getData
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's something else that works..
print 'I'm not working';
errors with Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'm'.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ';
print 'I''m working'; (two single quotes together)
print 'Double '' is only single' (two single quotes together)
Print 'Double '''' is truely double'; (four single quotes together)
Returns the following
I'm working
Double ' is only single
Double '' is truely double
print 'I'm not working';
errors with Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'm'.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ';
print 'I''m working'; (two single quotes together)
print 'Double '' is only single' (two single quotes together)
Print 'Double '''' is truely double'; (four single quotes together)
Returns the following
I'm working
Double ' is only single
Double '' is truely double
ASKER