How to check if an array is empty

Started by Tobias Anstettover 17 years ago2 messagesgeneral
Jump to latest
#1Tobias Anstett
tobias.anstett@iaas.uni-stuttgart.de

Hi,

in my special case I'd like to check if a XML[] is empty, because postgres
doesn't implement any equality operators, this is not a 'simple' task.

My current solution is:

SELECT * FROM sometable WHERE XMLSERIALIZE(CONTENT ((XPATH('/a:bar,
somexmlcolumn, ARRAY[ARRAY['a', 'http://foo]]))[1]) AS text)<>''

But there must be a better way - maybe I missed something important - I also
tried queries like the following:

SELECT * FROM sometable WHERE (XPATH('/a:bar', somexmlcolumn,
ARRAY[ARRAY['a', 'http://foo&#39;]]))&lt;&gt;(&#39;{}&#39;::XML[])

Any ideas and feedback are welcome.

Cheers, Tobias

#2Hitoshi Harada
umi.tanuki@gmail.com
In reply to: Tobias Anstett (#1)
Re: How to check if an array is empty

2008/9/11 Tobias Anstett <tobias.anstett@iaas.uni-stuttgart.de>:

Hi,

in my special case I'd like to check if a XML[] is empty, because postgres
doesn't implement any equality operators, this is not a 'simple' task.

My current solution is:

SELECT * FROM sometable WHERE XMLSERIALIZE(CONTENT ((XPATH('/a:bar,
somexmlcolumn, ARRAY[ARRAY['a', 'http://foo]]))[1]) AS text)<>''

But there must be a better way – maybe I missed something important - I also
tried queries like the following:

SELECT * FROM sometable WHERE (XPATH('/a:bar', somexmlcolumn,
ARRAY[ARRAY['a', 'http://foo&#39;]]))&lt;&gt;(&#39;{}&#39;::XML[])

Any ideas and feedback are welcome.

Cheers, Tobias

What about array_upper():

select array_upper('{}'::XML[], 1)

NULL will be returned if the array is empty in 1-dim.

--
Hitoshi Harada