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

What is the valid string representation of tstzrange[]?

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

Problem

I am trying to find string representation of tstzrange[] type (array of timestamp ranges):

select '{[2014-01-01,2015-01-01]}'::tstzrange[];
select '{''[2014-01-01,2015-01-01]''}'::tstzrange[];
select '{[''2014-01-01'',''2015-01-01'']}'::tstzrange[];


But each of queries above throws malformed range literal error.

I know that I can use array[tstzrange('2014-01-01','2015-01-01','[]')] but what I need is a string representation.

Solution

The thing is that here you have to combine two literals: '{}' for the array and '[...,...]' for the range type.

In all these cases, the easiest is just getting back the output from the system itself:

SELECT array[tstzrange('2014-01-01','2015-01-01','[]')];
                            array                            
─────────────────────────────────────────────────────────────
 {"[\"2014-01-01 00:00:00+01\",\"2015-01-01 00:00:00+01\"]"}


Now you simply wrap this into quotes, and there you are.

There is, however, a simpler way, which usually works: just double-quote the inner literal. See:

select '{"[2014-01-01,2015-01-01]"}'::tstzrange[];
                          tstzrange                          
─────────────────────────────────────────────────────────────
 {"[\"2014-01-01 00:00:00+01\",\"2015-01-01 00:00:00+01\"]"}


Note that the output is the same as above.

Code Snippets

SELECT array[tstzrange('2014-01-01','2015-01-01','[]')];
                            array                            
─────────────────────────────────────────────────────────────
 {"[\"2014-01-01 00:00:00+01\",\"2015-01-01 00:00:00+01\"]"}
select '{"[2014-01-01,2015-01-01]"}'::tstzrange[];
                          tstzrange                          
─────────────────────────────────────────────────────────────
 {"[\"2014-01-01 00:00:00+01\",\"2015-01-01 00:00:00+01\"]"}

Context

StackExchange Database Administrators Q#118758, answer score: 3

Revisions (0)

No revisions yet.