简体   繁体   中英

Query XML type data column and insert into SQL Server table

Hoping someone can help me with a SQL Server issue, In one of our tables I have a longtext data type which contains data in an XML type format as below:

<entry name=\"cleaned_sectors\" type=\"uint\">585937500</entry>\r\n<entry name=\"bad_sectors\" type=\"uint\">0</entry>\r\n<entry name=\"state\"

Generally when I have come across XML in a table it looks like this:

<header>data</Header><Manufacturer>Dell</Manufacturer>

And I generate a query to obtain the data like so:

SELECT 
    Tbl.Col.value('Header[1], 'nvarchar(50)'), Tbl.Col.value('manufacturer[1].'nvarchar(50)') 
FROM ...

However you will notice this data isn't standard XML and each tag is contained using \\" is there a way I can query the data contained with the aim to then import this into another table?

Expected output would be something like:

Cleaned Sectors | Bad Sectors
585937500       | 0

Any help with querying the data would be brilliant as I have been trying for hours. Thank you.

Clean your input like and then convert to XML and use as always.

SELECT
  CAST(
    REPLACE(
      REPLACE('<entry name=\"cleaned_sectors\" type=\"uint\">585937500</entry>\r\n'
      ,'\"', '"')
   ,'\r\n', '')
  AS XML)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM