sql server - CONVERTING SQL NVARCHAR(MAX) TO XML and getting a Value from XML string -


i trying read nodes of column -where xml string stored. column of type nvarchar(max).

the following script create table - set ansi_nulls on go

set quoted_identifier on go  create table [dbo].[xml_dummy](     [id] [int] identity(1,1) not null,     [name] [nvarchar](50) null,     [xmlvalue] [nvarchar](max) null ) on [primary] textimage_on [primary]  go 

the following script enter values -

insert [dbo].[xml_dummy]            ([name]            ,[xmlvalue])      values            ('abcd'            ,'<?xml version="1.0" encoding="utf-8"?>             <staffv xmlns:xsd="http://www.w3.org/2001/xmlschema"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">                  <employee_number>123456</employee_number>                 <title>mr</title>                 <initials>j</initials>                 <forename>pearl</forename>                 <surname>hoffman</surname>                 <gender>male</gender>                 <date_of_birth>1992-01-01</date_of_birth>                   <department_desc>super support team</department_desc>                   <job_title_desc>general dogsbody</job_title_desc>                   <original_date_joined>2014-05-01</original_date_joined>                   <current_employee financialyear="2014">y</current_employee>                   <internal_email xsi:nil="true" />                   <change_type>insert</change_type>                   <change_date>2014-03-27</change_date>             </staff>') go 

my goal gender string. start when writing following query -

     declare @xmlvaluecast_ xml     declare @xmlvalueconvert_ xml   select           @xmlvaluecast_ = cast(xmlvalue xml),          @xmlvalueconvert_ = convert(xml, xmlvalue)        --cast(cast(cast(xmlvalue ntext) xml).query('data(/staff/gender)') varchar(10)) gender         [dbo].[xml_dummy]      print @xmlvaluecast_     print @xmlvalueconvert_ 

i error - msg 9402, level 16, state 1, line 3 xml parsing: line 1, character 38, unable switch encoding.

i using sql server 2012 (11.0.5058.0)

first of why don't save column in xml? thing have error in tag. believe supposed staff, not staffv. , last thing utf-8 used instead of utf-16 xml encoding. final code is:

create table #xml_dummy     (       [id] [int] identity(1, 1)                  not null ,       [name] [nvarchar](50) null ,       [xmlvalue] [nvarchar](max) null     );   insert  #xml_dummy         ( [name] ,           [xmlvalue]         ) values         ( 'abcd' ,           '<?xml version="1.0" encoding="utf-8"?>             <staff xmlns:xsd="http://www.w3.org/2001/xmlschema"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">                  <employee_number>123456</employee_number>                 <title>mr</title>                 <initials>j</initials>                 <forename>pearl</forename>                 <surname>hoffman</surname>                 <gender>male</gender>                 <date_of_birth>1992-01-01</date_of_birth>                   <department_desc>super support team</department_desc>                   <job_title_desc>general dogsbody</job_title_desc>                   <original_date_joined>2014-05-01</original_date_joined>                   <current_employee financialyear="2014">y</current_employee>                   <internal_email xsi:nil="true" />                   <change_type>insert</change_type>                   <change_date>2014-03-27</change_date>             </staff>'         );  select     b.x.value('/staff[1]/gender[1]', 'varchar(100)')     #xml_dummy     cross apply (                   select                    cast(cast ([xmlvalue] varchar(max)) xml) x                 ) b; drop table #xml_dummy; 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -