简体   繁体   中英

Export value from nvarchar datatype column that saved like xml format

I want to export value from nvarchar datatype column that saved like xml format.

The nvarchar column value looks like this:

<TablePhoneHome><Number Num="1111"/></TablePhoneHome>

I want to export 1111 from that

Thanks

If the data is XML, you should really be using the xml data type, then this is trivial. I use CONVERT here, but you should really fix the data type in your table.

CONVERT(xml, YourColumn).value('(TablePhoneHome/Number/@Num)[1]','int')

Like I said though, really you should change your table's column data type:

CREATE TABLE YourTable (YourColumn nvarchar(MAX));

INSERT INTO YourTable
VALUES('<TablePhoneHome><Number Num="1111"/></TablePhoneHome>');

ALTER TABLE YourTable ALTER COLUMN YourColumn xml;

SELECT YourColumn.value('(TablePhoneHome/Number/@Num)[1]','int') AS Num
FROM YourTable;

DB<>Fiddle

if all of the values is same xml format, use with replace function in sql-server . try this:

select  REPLACE(REPLACE('<TablePhoneHome><Number Num="1111"/></TablePhoneHome>','<TablePhoneHome><Number Num="' ,'') ,'"/></TablePhoneHome>','')

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