6

I recently discovered that a time range can be empty which appears to be different vs a null field.

db=# select tstzrange(now(), now());
 tstzrange
-----------
 empty
(1 row)

db=# select tstzrange(now(), now()) is null;
 ?column?
----------
 f
(1 row)

How can I match an empty field for a query?

2 Answers 2

10

Compare it with a string 'empty'

select tstzrange(now(), now()) = 'empty' as out

which produces:

 out
-----
 t

While for a non-empty result with query like:

select tstzrange(now(), now() + interval '1d') = 'empty' as out

it consistently produces:

 out
-----
 f

In fact implicit conversion of 'empty'::tstzrange takes place.

Read more on the matter in documentation

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Thank you! I'm curious why it works this way, how can a tstzrange type eval against a string 'empty'?
See how it's implemented: postgresql.org/docs/current/static/… There is an implicit conversion of 'empty' to tstzrange type.
3

use isempty()

select isempty(tstzrange(now(), now())); -- true
select isempty(tstzrange(now(), now() + interval '1d')); -- false

1 Comment

This also seems to work for multiranges (at least in postgres 15), making it a more useful answer than relying on the implicit cast from string to range.

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.