sureshbabukrish
asked on
Replace NULL value with Blank space in Xquery
DECLARE @xml XML
,@aValue VARCHAR(10)
,@bValue VARCHAR(10)
,@cValue VARCHAR(10)
,@dValue VARCHAR(10)
,@eValue VARCHAR(10)
,@fValue VARCHAR(10)
SET @xml =
'<Test>
<Test1>
<a>NA</a>
<b>NA</b>
</Test1>
<Test1>
<a>NA</a>
<b>NA</b>
</Test1>
<Test1>
<a>NA</a>
<b>NA</b>
</Test1>
</Test>'
SELECT
@aValue = COALESCE(NULL,'')
,@bValue = COALESCE(25,'')
,@cValue = COALESCE(NULL,'')
,@dValue = COALESCE(50,'')
,@eValue = COALESCE(NULL,'')
,@fValue = COALESCE(100,'')
SET @xml.modify('replace value of (//Test/Test1/a/text())[1]
with sql:variable("@aValue")')
SET @xml.modify('replace value of (//Test/Test1/b/text())[1]
with sql:variable("@bValue")')
SET @xml.modify('replace value of (//Test/Test1/a/text())[2]
with sql:variable("@cValue")')
SET @xml.modify('replace value of (//Test/Test1/b/text())[2]
with sql:variable("@dValue")')
SET @xml.modify('replace value of (//Test/Test1/a/text())[3]
with sql:variable("@eValue")')
SET @xml.modify('replace value of (//Test/Test1/b/text())[3]
with sql:variable("@fValue")')
SELECT @xml
I need to replace the NULL value with empty space. But '' works as the escape character and hence NA is not replaced at node level 2 (@cValue). Please suggest.
,@aValue VARCHAR(10)
,@bValue VARCHAR(10)
,@cValue VARCHAR(10)
,@dValue VARCHAR(10)
,@eValue VARCHAR(10)
,@fValue VARCHAR(10)
SET @xml =
'<Test>
<Test1>
<a>NA</a>
<b>NA</b>
</Test1>
<Test1>
<a>NA</a>
<b>NA</b>
</Test1>
<Test1>
<a>NA</a>
<b>NA</b>
</Test1>
</Test>'
SELECT
@aValue = COALESCE(NULL,'')
,@bValue = COALESCE(25,'')
,@cValue = COALESCE(NULL,'')
,@dValue = COALESCE(50,'')
,@eValue = COALESCE(NULL,'')
,@fValue = COALESCE(100,'')
SET @xml.modify('replace value of (//Test/Test1/a/text())[1]
with sql:variable("@aValue")')
SET @xml.modify('replace value of (//Test/Test1/b/text())[1]
with sql:variable("@bValue")')
SET @xml.modify('replace value of (//Test/Test1/a/text())[2]
with sql:variable("@cValue")')
SET @xml.modify('replace value of (//Test/Test1/b/text())[2]
with sql:variable("@dValue")')
SET @xml.modify('replace value of (//Test/Test1/a/text())[3]
with sql:variable("@eValue")')
SET @xml.modify('replace value of (//Test/Test1/b/text())[3]
with sql:variable("@fValue")')
SELECT @xml
I need to replace the NULL value with empty space. But '' works as the escape character and hence NA is not replaced at node level 2 (@cValue). Please suggest.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Solution was provided which is valid and solves the question - useful for PAQ database.
http:#a33518594 describes the problem and why sequential updating won't work, especially with a path-positional based approach like XQuery XPath.
http:#a33632784 provides a solution to perform the required updates.
Split between the two.
http:#a33632784 provides a solution to perform the required updates.
Split between the two.
ASKER
the answers were useful
SET @xml.modify('replace value of (//Test/Test1/a/text())[2]
a/text()[2] is actually hitting the <a id="3"> node.
By the time it gets to
(//Test/Test1/a/text())[3]
There is no "3rd" element of a/text().
Open in new window