string_to_array with empty input - Mailing list pgsql-general

From Steve Crawford
Subject string_to_array with empty input
Date
Msg-id 49D115B9.7030801@pinpointresearch.com
Whole thread Raw
Responses Re: string_to_array with empty input  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
I have a query that converts a string to an array with the
string_to_array function. Sometimes the input is an empty string (not a
null, but a string of zero-length). I had expected the result to be a
one-element array with an empty string as the first and only element but
instead it returned null. I looked at the docs and didn't find the
observed behavior documented.

I ran some tests:
string_to_array(null,',') -- expected and got null
string_to_array('a',',') -- expected and got one-element array {a}
string_to_array('',',') -- expected consistency with above (one-element
array with empty string) but got null

So I looked to see how array_to_string deals with such arrays:
array_to_string('{""}'::text[],',')  -- expected and got an empty string

I realize that array_to_string is not a perfect inverse of
string_to_array - especially due to the fact that the array-index is not
preserved. But it seems that the array data should match.

Is this a bug or am I missing the logic behind this behavior? As it
stands, it appears I will have to wrap the function in a case statement
as a work-around unless there is a better solution.

Cheers,
Steve


pgsql-general by date:

Previous
From: A B
Date:
Subject: Re: [GENEAL] dynamically changing table
Next
From: Dave Page
Date:
Subject: Re: PostgreSql with or without Plus?