3

I'm converting an xml string to a table type:

SELECT
[Guid] = r.value('(Guid)[1]', 'uniqueidentifier'),
[Id] = r.value('(Id)[1]', 'int'),
[SomeColumn] = r.value('(SomeColumn)[1]', 'int'),
FROM
@xml.nodes('//Items') AS T(r)

Where SomeColumn is a nullable int. The xml that is passed is the following for this element:

<SomeColumn xsi:nil="true" />

However, the column is set to 0 instead of null. Is there a way to enforce to return null on nullable columns that are actually null?

1
  • 2
    Can we have a minimal but complete sample piece of XML that the above query can be run against please? Commented Mar 9, 2017 at 13:08

1 Answer 1

3

Well, you can retrieve it as varchar instead of int and then check for emptiness of retrieved string something like:

SELECT
    [Guid] = r.value('(Guid)[1]', 'uniqueidentifier'),
    [Id] = r.value('(Id)[1]', 'int'),
    [SomeColumn] = cast(nullif(r.value('(SomeColumn)[1]', 'varchar(20)'), '') as int)
FROM
    @xml.nodes('//Items') AS T(r)
Sign up to request clarification or add additional context in comments.

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.