pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Selecting rows having substring in a column
7+ messages / 4 participants
[nested] [flat]

* Selecting rows having substring in a column
@ 2019-08-29 14:13 Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-29 14:25 ` Re: Selecting rows having substring in a column Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-29 14:28 ` Re: Selecting rows having substring in a column Gary Cowell <gary.cowell+pgsql@gmail.com>
  0 siblings, 2 replies; 7+ messages in thread

From: Rich Shepard @ 2019-08-29 14:13 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Using postgres-11.1 here. My SQL knowledge needs expanding and my web
searches have not found a satisfactory answer. I hope to learn the correct
approach here.

A table (Fishes) has an attribute column stream_trib with values such as
 	Small Creek trib to Winding River
 	Roaring River trib to Winding River
and I want to find all rows containing Winding River in that column.

The postgres substring function takes as arguments the substring, starting
position, and length. In my table the staring position varies although the
length remains constant.

I need to learn how to construct a SELECT statement that returns the set of
rows containing the substring 'Winding River'. A pointer to references would
be great; so would a detailed lesson in handling this and similar queries.

Regards,

Rich





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Selecting rows having substring in a column
  2019-08-29 14:13 Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-29 14:25 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-29 14:46   ` Re: Selecting rows having substring in a column [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Adrian Klaver @ 2019-08-29 14:25 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general@lists.postgresql.org

On 8/29/19 7:13 AM, Rich Shepard wrote:
> Using postgres-11.1 here. My SQL knowledge needs expanding and my web
> searches have not found a satisfactory answer. I hope to learn the correct
> approach here.
> 
> A table (Fishes) has an attribute column stream_trib with values such as
>      Small Creek trib to Winding River
>      Roaring River trib to Winding River
> and I want to find all rows containing Winding River in that column.
> 
> The postgres substring function takes as arguments the substring, starting
> position, and length. In my table the staring position varies although the
> length remains constant.
> 
> I need to learn how to construct a SELECT statement that returns the set of
> rows containing the substring 'Winding River'. A pointer to references 
> would
> be great; so would a detailed lesson in handling this and similar queries.

https://www.postgresql.org/docs/11/functions-matching.html

create table like_test(fld_1 varchar);

insert into like_test values ('Small Creek trib to Winding River');
insert into like_test values ('Roaring River trib to Winding River');
insert into like_test values ('Roaring River');

test=# select * from like_test where  fld_1 ilike '%Winding River%';
                 fld_1
-------------------------------------
  Small Creek trib to Winding River
  Roaring River trib to Winding River
(2 rows)

Using ilike to case-insensitive search.

If you want more in depth search:

https://www.postgresql.org/docs/11/functions-textsearch.html

>  > Regards,
> 
> Rich
> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Selecting rows having substring in a column [RESOLVED]
  2019-08-29 14:13 Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-29 14:25 ` Re: Selecting rows having substring in a column Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-08-29 14:46   ` Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-29 16:02     ` Re: Selecting rows having substring in a column [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Rich Shepard @ 2019-08-29 14:46 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Thu, 29 Aug 2019, Adrian Klaver wrote:

> test=# select * from like_test where  fld_1 ilike '%Winding River%';
>                fld_1

Adrian,

Aha! I thought of 'like' but forgot about ilike. That's exactly what I need.

Thanks very much,

Rich





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Selecting rows having substring in a column [RESOLVED]
  2019-08-29 14:13 Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-29 14:25 ` Re: Selecting rows having substring in a column Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-29 14:46   ` Re: Selecting rows having substring in a column [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-29 16:02     ` Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Rich Shepard @ 2019-08-29 16:02 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Thu, 29 Aug 2019, Rich Shepard wrote:

> Aha! I thought of 'like' but forgot about ilike. That's exactly what I
> need.

'thought' is the wrong word. I should have written that I once knew of like
and had forgotten it.

Rich






^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Selecting rows having substring in a column
  2019-08-29 14:13 Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-29 14:28 ` Gary Cowell <gary.cowell+pgsql@gmail.com>
  2019-08-29 14:47   ` Re: Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Gary Cowell @ 2019-08-29 14:28 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org

Look at the 'LIKE' function

select * from Fishes where stream_trib like '%Winding River%';

You need to 'bookend' your string with '%' for 'LIKE' function, if the
string could match anywhere. If it could only be at the end, you could
use '%Winding River'

If case is an issue, wrap it with upper (or lower)

.... where upper(stream_trib) like '%WINDING RIVER%';

You could also use a regular expression, or 'SIMILAR', but 'LIKE' is
often the simplest.

More information here:

https://www.postgresql.org/docs/11/functions-matching.html

On Thu, 29 Aug 2019 at 15:13, Rich Shepard <rshepard@appl-ecosys.com> wrote:
>
> Using postgres-11.1 here. My SQL knowledge needs expanding and my web
> searches have not found a satisfactory answer. I hope to learn the correct
> approach here.
>
> A table (Fishes) has an attribute column stream_trib with values such as
>         Small Creek trib to Winding River
>         Roaring River trib to Winding River
> and I want to find all rows containing Winding River in that column.
>
> The postgres substring function takes as arguments the substring, starting
> position, and length. In my table the staring position varies although the
> length remains constant.
>
> I need to learn how to construct a SELECT statement that returns the set of
> rows containing the substring 'Winding River'. A pointer to references would
> be great; so would a detailed lesson in handling this and similar queries.
>
> Regards,
>
> Rich
>
>





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Selecting rows having substring in a column
  2019-08-29 14:13 Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-29 14:28 ` Re: Selecting rows having substring in a column Gary Cowell <gary.cowell+pgsql@gmail.com>
@ 2019-08-29 14:47   ` Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-29 15:15     ` Re: Selecting rows having substring in a column Rob Sargent <robjsargent@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Rich Shepard @ 2019-08-29 14:47 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Thu, 29 Aug 2019, Gary Cowell wrote:

> Look at the 'LIKE' function

Gary,

Yes, I thought of like but didn't think to look for it in the postgres
manual.

Thank you very much,

Rich





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Selecting rows having substring in a column
  2019-08-29 14:13 Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-29 14:28 ` Re: Selecting rows having substring in a column Gary Cowell <gary.cowell+pgsql@gmail.com>
  2019-08-29 14:47   ` Re: Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-29 15:15     ` Rob Sargent <robjsargent@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Rob Sargent @ 2019-08-29 15:15 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


On 8/29/19 8:47 AM, Rich Shepard wrote:
> On Thu, 29 Aug 2019, Gary Cowell wrote:
>
>> Look at the 'LIKE' function
>
> Gary,
>
> Yes, I thought of like but didn't think to look for it in the postgres
> manual.
>
> Thank you very much,
>
> Rich
>
>
I've given up on the "likes" in favour of the ~ (tilde) and ~* (tilde 
asterisk) operator.  Way cool, powerful. Not standard sql though (iirc)





^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2019-08-29 16:02 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-29 14:13 Selecting rows having substring in a column Rich Shepard <rshepard@appl-ecosys.com>
2019-08-29 14:25 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-08-29 14:46   ` Re: Selecting rows having substring in a column [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
2019-08-29 16:02     ` Re: Selecting rows having substring in a column [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
2019-08-29 14:28 ` Gary Cowell <gary.cowell+pgsql@gmail.com>
2019-08-29 14:47   ` Rich Shepard <rshepard@appl-ecosys.com>
2019-08-29 15:15     ` Rob Sargent <robjsargent@gmail.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