fahadash
fahadash

Reputation: 3281

How to use XQuery to fetch NULL values for INT

I have the following example

DECLARE @MyXml XML 

SET @MyXml = CONVERT(XML, '<?xml version="1.0" ?>
                            <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                                <number xsi:nil="true" />
                            </root>')

SELECT @MyXml.value('(/root/number)[1]', 'INT') AS MyNumber

The above listing gives me a 0 instead of a NULL that I would expect. I know I am using Schema Instance, but shouldn't it be a widely accepted standard already?

Upvotes: 2

Views: 314

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175934

You can use text():

LiveDemo

DECLARE @MyXml XML 

SET @MyXml = CONVERT(XML, 
  '<?xml version="1.0" ?>
   <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <number xsi:nil="true" />
  </root>')

SELECT @MyXml.value('(/root/number/text())[1]', 'INT') AS MyNumber

Upvotes: 1

Related Questions