pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to allow null as an option when using regexp_matches?
5+ messages / 4 participants
[nested] [flat]
* How to allow null as an option when using regexp_matches?
@ 2021-12-08 12:07 Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: Shaozhong SHI @ 2021-12-08 12:07 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
We can do this:
select count(*) from regexp_matches('Great London', 'Great
London|Information Centre|Department for Transport', 'g');
Is it possible to allow null as an option? something like this
select count(*) from regexp_matches('Great London', 'null|Great
London|Information Centre|Department for Transport', 'g');
Regards,
David
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to allow null as an option when using regexp_matches?
@ 2021-12-08 12:09 Karsten Hilbert <Karsten.Hilbert@gmx.net>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Karsten Hilbert @ 2021-12-08 12:09 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Am Wed, Dec 08, 2021 at 12:07:13PM +0000 schrieb Shaozhong SHI:
> We can do this:
> select count(*) from regexp_matches('Great London', 'Great
> London|Information Centre|Department for Transport', 'g');
>
> Is it possible to allow null as an option? something like this
> select count(*) from regexp_matches('Great London', 'null|Great
> London|Information Centre|Department for Transport', 'g');
You seem to want to apply coalesce() judiciously.
Best,
Karsten
--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to allow null as an option when using regexp_matches?
@ 2021-12-08 17:07 Shaozhong SHI <shishaozhong@gmail.com>
parent: Karsten Hilbert <Karsten.Hilbert@gmx.net>
0 siblings, 1 reply; 5+ messages in thread
From: Shaozhong SHI @ 2021-12-08 17:07 UTC (permalink / raw)
To: Karsten Hilbert <Karsten.Hilbert@gmx.net>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Hi, Karsten,
That sounds interesting.
Any good example?
Regards, David
On Wed, 8 Dec 2021 at 12:10, Karsten Hilbert <Karsten.Hilbert@gmx.net>
wrote:
> Am Wed, Dec 08, 2021 at 12:07:13PM +0000 schrieb Shaozhong SHI:
>
> > We can do this:
> > select count(*) from regexp_matches('Great London', 'Great
> > London|Information Centre|Department for Transport', 'g');
> >
> > Is it possible to allow null as an option? something like this
> > select count(*) from regexp_matches('Great London', 'null|Great
> > London|Information Centre|Department for Transport', 'g');
>
> You seem to want to apply coalesce() judiciously.
>
> Best,
> Karsten
> --
> GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B
>
>
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to allow null as an option when using regexp_matches?
@ 2021-12-08 17:40 Vincent Veyron <vv.lists@wanadoo.fr>
parent: Shaozhong SHI <shishaozhong@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Vincent Veyron @ 2021-12-08 17:40 UTC (permalink / raw)
To: Shaozhong SHI <shishaozhong@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
On Wed, 8 Dec 2021 17:07:27 +0000
Shaozhong SHI <shishaozhong@gmail.com> wrote:
>
> Any good example?
>
Plenty of them in the fine documentation :
https://www.postgresql.org/search/?q=coalesce
-- Bien à vous, Vincent Veyron
https://compta.libremen.com
Logiciel libre de comptabilité générale en partie double
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to allow null as an option when using regexp_matches?
@ 2021-12-08 17:59 Roxanne Reid-Bennett <rox@tara-lu.com>
parent: Shaozhong SHI <shishaozhong@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Roxanne Reid-Bennett @ 2021-12-08 17:59 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On 12/8/2021 4:07 AM, Shaozhong SHI wrote:
> We can do this:
> select count(*) from regexp_matches('Great London', 'Great London|Information Centre|Department for Transport', 'g');
>
> Is it possible to allow null as an option? something like this
> select count(*) from regexp_matches('Great London', 'null|Great London|Information Centre|Department for Transport', 'g');
>
> Regards,
>
> David
>
Hi David,
I'm assuming that 'Great London' is coming from some column value.
Given that NULL is a state, not a value, regexp really cannot "find" or not "find" it.
you could use COALESCE the source of 'Great London' to a predictable value that you CAN match on.
or you could possibly construct your query something like this:
select CASE WHEN 'Great London' IS NULL THEN 1 ELSE 0 END + (SELECT count(*) from regexp_matches('Great London', 'Great London|Information Centre|Department for Transport', 'g'))
select CASE WHEN NULL IS NULL THEN 1 ELSE 0 END + (SELECT count(*) from regexp_matches(NULL, 'Great London|Information Centre|Department for Transport', 'g'))
Interestingly to me, the following returns 2 - possibly because an empty string matches anything?
select count(*) from regexp_matches('Great London', 'Great London||Information Centre|Department for Transport', 'g');
Roxanne
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2021-12-08 17:59 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-12-08 12:07 How to allow null as an option when using regexp_matches? Shaozhong SHI <shishaozhong@gmail.com>
2021-12-08 12:09 ` Karsten Hilbert <Karsten.Hilbert@gmx.net>
2021-12-08 17:07 ` Shaozhong SHI <shishaozhong@gmail.com>
2021-12-08 17:40 ` Vincent Veyron <vv.lists@wanadoo.fr>
2021-12-08 17:59 ` Roxanne Reid-Bennett <rox@tara-lu.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox