HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Why is an empty XML node xml datatype not being recognized in this query?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thiswhydatatypenodequeryemptyxmlbeingrecognizednot

Problem

My query seems to not recognize self-closing or empty nodes at all. Shouldn't the empty ` node return 'null'? Or an empty string. Even if I place an empty space in the node, it still is ignored.

declare @x xml; 

    set @x='
                
                    
                    sdfg
                    dfgh
                
                
                    foo
                    bar
                    poo
                
            '
select
    x.c.value('(td//text())[1]', 'varchar(1024)') as col1
    ,x.c.value('(td//text())[2]', 'varchar(1024)') as col2
    ,x.c.value('(td//text())[3]', 'varchar(1024)') as col3
from @x.nodes('/root/tr') x(c)


I want the result to be this:

col1 col2 col3
null sdfg dfgh
foo bar poo


..but instead I get this:

col1 col2 col3
sdfg dfgh null
foo bar poo
`

Solution

In (td//text())[1] the predicate [1] will return the first node returned by the XQuery (td//text()).

You are saying that you want the first text() node. But in the first td element there are no text nodes present so you get the text node for the second td element which is the first text node returned.

You need to specify the position predicate on the td node instead.

This will give you null in col1

select
     x.c.value('td[1]/text()[1]', 'varchar(1024)') as col1
    ,x.c.value('td[2]/text()[1]', 'varchar(1024)') as col2
    ,x.c.value('td[3]/text()[1]', 'varchar(1024)') as col3
from @x.nodes('/root/tr') x(c)


And not specifying the text() node will give you an empty string in col1.

select
     x.c.value('td[1]', 'varchar(1024)') as col1
    ,x.c.value('td[2]', 'varchar(1024)') as col2
    ,x.c.value('td[3]', 'varchar(1024)') as col3
from @x.nodes('/root/tr') x(c)

Code Snippets

select
     x.c.value('td[1]/text()[1]', 'varchar(1024)') as col1
    ,x.c.value('td[2]/text()[1]', 'varchar(1024)') as col2
    ,x.c.value('td[3]/text()[1]', 'varchar(1024)') as col3
from @x.nodes('/root/tr') x(c)
select
     x.c.value('td[1]', 'varchar(1024)') as col1
    ,x.c.value('td[2]', 'varchar(1024)') as col2
    ,x.c.value('td[3]', 'varchar(1024)') as col3
from @x.nodes('/root/tr') x(c)

Context

StackExchange Database Administrators Q#240329, answer score: 4

Revisions (0)

No revisions yet.