9

I have a sql query that is kicking back with an error on my column name saying 'cannot call methods on nvarchar(max).

      SELECT [LEARNER_COURSE_XML_TEST].[XML_EX].Query('declare namespace
      x="http://tempuri.org/cmi.xsd";] (/x:cmi/x:core/x:time_taken)') 
      AS TimeTaken FROM [LEARNER_COURSE_XML_TEST]

The issue seems to centre around [XML_EX].value but I've tried a few things including changing the column type but i've finally come unstuck. Any pointers would be greatly appreciated.

2 Answers 2

9

Sounds like XML_EX is of type nvarchar(max). Try changing it to xml.

You can also cast it in the query, like so:

select  cast(lcxt.XML_EX as xml).query(...)
from    learner_course_xml_test lcxt
Sign up to request clarification or add additional context in comments.

2 Comments

CAST has to wrap the whole column reference, not just the column name (i.e. lcxt. should be inside)
@Damien_The_Unbeliever: Obviously right, feel free to edit ;)
1

Thanks for your responses guys. Turns out I was over complicating it as I don't have access to my namespace in the SQL table. I did however start by changing my field type to XML so thanks Andomar. My solution is below:

SELECT [LEARNER_COURSE_XML_TEST].[XML_EX].query('data(sco/cmicore/total_time)') AS  TimeTaken FROM [LEARNER_COURSE_XML_TEST] 

This extracts my total times as i'd hoped. Thanks again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.