Re: Trouble with regexp_matches

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Edson Richter <edsonrichter(at)hotmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Trouble with regexp_matches
Date: 2016-11-05 17:21:07
Message-ID: 31701.1478366467@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Edson Richter <edsonrichter(at)hotmail(dot)com> writes:
> I’m running the query below, and it is limiting results as if “regexp_matches” being in where clause.
> IMHO, it is wrong: in case there is no match, shall return null or empty array – not remove the result from the set!!!

Well, no, because regexp_matches() returns a set. If there's no match,
there's zero rows in the set.

The standard workaround is to use a scalar sub-select, which has the
effect of converting a zero-row result into a NULL:

select codigoocorrencia, datahoraocorrencia, datahoraimportacao,
observacao, (select regexp_matches(observacao, '\d\d/\d\d/\d\d\d\d'))
from ...

As of v10 there will be a less confusing solution: use regexp_match()
instead.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Edson Richter 2016-11-05 18:08:27 RES: Trouble with regexp_matches
Previous Message Adrian Klaver 2016-11-05 17:13:25 Re: Trouble with regexp_matches