Replace “<” and “>” with “<” and “>” in sql server
Asked Answered
F

7

14

Hi I am new to for xml

I have a query like this

SELECT  ProjectId, 
       ProjectCode, 
       ProjectName, 
       TechId, 
      -- LocationId, 

      ( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
       t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
    FROM GeographicLocation t2
    WHERE GeoId = t1.LocationId
    FOR XML  PATH('Location') ),
       RtoId, 
       CreatedBy,
       CreatedOn,
       LastUpdatedBy,
       LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')

it return the xml as

<ProjectInfo>
<ProjectId>1</ProjectId>
<ProjectCode>US-W1-00001</ProjectCode>
<ProjectName>Rees</ProjectName>
<TechId>1</TechId>
&lt;Location&gt;&lt;GeoId&gt;235&lt;/GeoId&gt;&lt;PoliticalDivisionId&gt;2&lt;/PoliticalDivisionId&gt;&lt;GeographicLocationName&gt;UNITED STATES&lt;/GeographicLocationName&gt;&lt;IsoCode&gt;US&lt;/IsoCode&gt;&lt;/Location&gt;
<RtoId>3</RtoId>
<CreatedBy>1</CreatedBy>
<CreatedOn>2013-06-30T20:55:21.587</CreatedOn>
<LastUpdatedBy>1</LastUpdatedBy>
<LastUpdatedOn>2013-06-30T20:55:21.587</LastUpdatedOn>

prject tags are shown in the form < and > . But inner tags of Location are shown as “<” and “>” how do I replace them with < and >

Update : there was a small error in the question . inner xml was not for rtoid , it was for Location

I updated query as

SELECT  ProjectId, 
       ProjectCode, 
       ProjectName, 
       TechId, 
      -- LocationId, 

      replace(replace( ( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
       t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
    FROM GeographicLocation t2
    WHERE GeoId = t1.LocationId
    FOR XML  PATH('Location') ), '&lt;', '<'), '&gt;', '>'),
       RtoId, 
       CreatedBy,
       CreatedOn,
       LastUpdatedBy,
       LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')

but still its the same

Forfeiture answered 6/7, 2013 at 14:7 Comment(0)
T
19

I think correct way is using TYPE Directive

SELECT  ProjectId, 
        ...,
      ( SELECT Geo, ...
        FROM GeographicLocation t2
        WHERE GeoId = t1.LocationId
        FOR XML  PATH('Location'), TYPE),
       RtoId,                      ^^^^
       ...
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo') 
Transubstantiation answered 26/6, 2014 at 8:15 Comment(1)
This is the correct answer - I left all my old code behind at my last company, and this answer kept me from pulling my hair out. Many thanks!Cinnabar
J
15

The way that I've found is with explicitly replacing them:

select ProjectId, ProjectCode, ProjectName, TechId,
       replace(replace(RtoId, '&lt;', '<'), '&gt;', '>') as RtoId, 
       . . .
from (<your query here>)
Jobina answered 6/7, 2013 at 14:18 Comment(0)
T
3
    SELECT  ProjectId, 
       ProjectCode, 
       ProjectName, 
       TechId, 
      -- LocationId, 
      replace(replace(( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
       t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
    FROM GeographicLocation t2
    WHERE GeoId = t1.LocationId
    FOR XML  PATH('Location') ), '&lt;', '<'), '&gt;', '>')
       RtoId, 
       CreatedBy,
       CreatedOn,
       LastUpdatedBy,
       LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
Tenebrific answered 6/7, 2013 at 14:23 Comment(0)
F
3

format the data into xml, use cast(@xml as xml).

Fancie answered 20/5, 2016 at 9:16 Comment(1)
This doesn't work for large XML data. For me it would only go up to 200 characters for replaying the &lt; and &gt; with < and >.Unwholesome
A
3

Please try:

(SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,
        Longitude,Latitude,ParentLocationId,
        t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
    FROM GeographicLocation t2
    WHERE GeoId = t1.LocationId
        FOR XML  PATH('Location'), type
        ).value('(./text())[1]','varchar(max)')
Abutilon answered 7/2, 2018 at 13:3 Comment(2)
This quite simple answer (with minor change to the value function) allowed me to concatenate a series of XML strings without getting ugly gt;s and lt;s, and I think this method is more reliable than doing a replace().Fergus
Excellent answer. More info about the Value() method: learn.microsoft.com/en-us/sql/t-sql/xml/… One can also replace the parameter './text())[1]' with a simple '.' for the same effect.Meldameldoh
C
1
SELECT ProjectId,
       ProjectCode, 
       ProjectName, 
       TechId, 
      -- LocationId, 
      cast(( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId,
       t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn
    FROM GeographicLocation t2
    WHERE GeoId = t1.LocationId
    FOR XML  PATH('Location') ) as xml),
       RtoId, 
       CreatedBy,
       CreatedOn,
       LastUpdatedBy,
       LastUpdatedOn
FROM Project t1
where ProjectId=1
FOR XML PATH('ProjectInfo')
Cavalla answered 21/6, 2018 at 12:38 Comment(0)
C
0

Use FOR XML

SELECT customerid 
  , name
  , SUBSTRING( x, 4, LEN( x) - 7)        AS name2
  , IIF( LEN(name) <> LEN(x) - 7, 1, 0)  AS residual 
FROM (SELECT customerid
        ,   name
        ,   (SELECT kundnamn AS n FOR XML PATH('')) as x
      FROM customers
      ) s

or

declare @s varchar(MAX) = 'asd& < > " '' ='
PRINT @s
declare @xml varchar(MAX)
SELECT @xml = SUBSTRING( x, 4, LEN( x) - 7)
FROM (SELECT (SELECT @s AS x FOR XML PATH('')) AS x ) x
PRINT @xml

gives

asd& < > " ' =
asd&amp; &lt; &gt; " ' =
Camphene answered 9/2, 2021 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.