Обсуждение: [NOVICE] Trouble with IN operator

Поиск
Список
Период
Сортировка

[NOVICE] Trouble with IN operator

От
Chuck Roberts
Дата:

I'm having trouble with the IN operator when querying a Postgresql db using
the psql command line tool. Now that I think of it, I've never gotten the
IN operator to work before. And now it's critical. I'm using the psql tool
in a Windows 7 command window, version is 7.4.6.

I wonder if I'm missing something basic, which is not even in the manuals
I've been reading. I want to find a string field in an array of strings. I
want to limit the costcenter I'm selecting on and the costcenter is a
string. I've already googled to find some help but have found no info or
examples on using IN with an array of strings. So here's what I'm trying to
do:

AND (TRIM(tbl.costcenter)  IN ('540'))

This results on no error, but also no records. When I take this simple
clause out, I get records. I've also tried using regex, and get the same
result: no records are returned.

AND ((tbl.costcenter ~* '^(540|001)$')  )

Can anyone shed some light on this for me?

Thank you.



Re: [NOVICE] Trouble with IN operator

От
"David G. Johnston"
Дата:
On Fri, Feb 3, 2017 at 10:12 AM, Chuck Roberts <croberts@gilsongraphics.com> wrote:

AND (TRIM(tbl.costcenter)  IN ('540'))

Can anyone shed some light on this for me?


​It works...

​WITH tbl (id, costcenter) AS ( VALUES (1, '500'), (2, '550') )
SELECT *
FROM tbl 
WHERE (trim(tbl.costcenter) IN ('550'));

So the problem resides in something you haven't show us - namely data.

David J.


Re: [NOVICE] Trouble with IN operator

От
Chuck Roberts
Дата:
This is a vendor's database and we don't have control over it, nor do we have direct access to the Postgresql table layouts. We are given a spreadsheet to show us what the data types are. 

As for the data, sometimes tbl.costcenter can be blank on some records. Surely that wouldn't interfere with the IN operator? 

Chuck


Thanks!

Chuck Roberts
Gilson Graphics | Composition Dept.
p:616-459-4539 x 1228


On Fri, Feb 3, 2017 at 12:37 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Fri, Feb 3, 2017 at 10:12 AM, Chuck Roberts <croberts@gilsongraphics.com> wrote:

AND (TRIM(tbl.costcenter)  IN ('540'))

Can anyone shed some light on this for me?


​It works...

​WITH tbl (id, costcenter) AS ( VALUES (1, '500'), (2, '550') )
SELECT *
FROM tbl 
WHERE (trim(tbl.costcenter) IN ('550'));

So the problem resides in something you haven't show us - namely data.

David J.



Re: [NOVICE] Trouble with IN operator

От
Chris Campbell
Дата:

Empty String or Null?

 

Try:  trim(Coalesce(tbl.costcenter,’’)) IN …

 

 

Chris Campbell | Software Architect

C A S C A D E   D A T A  S O L U T I O N S

(800) 280-2090

www.cascadeds.com

 

From: pgsql-novice-owner@postgresql.org [mailto:pgsql-novice-owner@postgresql.org] On Behalf Of Chuck Roberts
Sent: Friday, February 3, 2017 10:08 AM
To: PSql novice list <pgsql-novice@postgresql.org>
Subject: Re: [NOVICE] Trouble with IN operator

 

This is a vendor's database and we don't have control over it, nor do we have direct access to the Postgresql table layouts. We are given a spreadsheet to show us what the data types are. 

 

As for the data, sometimes tbl.costcenter can be blank on some records. Surely that wouldn't interfere with the IN operator? 

 

Chuck

 


Thanks!

 

Chuck Roberts

Gilson Graphics | Composition Dept.

p:616-459-4539 x 1228

 

On Fri, Feb 3, 2017 at 12:37 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:

On Fri, Feb 3, 2017 at 10:12 AM, Chuck Roberts <croberts@gilsongraphics.com> wrote:

 

AND (TRIM(tbl.costcenter)  IN ('540'))

 

Can anyone shed some light on this for me?

 

 

​It works...

 

​WITH tbl (id, costcenter) AS ( VALUES (1, '500'), (2, '550') )

SELECT *

FROM tbl 

WHERE (trim(tbl.costcenter) IN ('550'));

 

So the problem resides in something you haven't show us - namely data.

 

David J.

 

 

 

Re: [NOVICE] Trouble with IN operator

От
Chuck Roberts
Дата:
I think this might be a data problem. If anyone else has an idea, it's welcome. 


Thanks!

Chuck Roberts
Gilson Graphics | Composition Dept.
p:616-459-4539 x 1228


On Fri, Feb 3, 2017 at 1:14 PM, Chris Campbell <ccampbell@cascadeds.com> wrote:

Empty String or Null?

 

Try:  trim(Coalesce(tbl.costcenter,’’)) IN …

 

 

Chris Campbell | Software Architect

C A S C A D E   D A T A  S O L U T I O N S

(800) 280-2090

www.cascadeds.com

 

From: pgsql-novice-owner@postgresql.org [mailto:pgsql-novice-owner@postgresql.org] On Behalf Of Chuck Roberts
Sent: Friday, February 3, 2017 10:08 AM
To: PSql novice list <pgsql-novice@postgresql.org>
Subject: Re: [NOVICE] Trouble with IN operator

 

This is a vendor's database and we don't have control over it, nor do we have direct access to the Postgresql table layouts. We are given a spreadsheet to show us what the data types are. 

 

As for the data, sometimes tbl.costcenter can be blank on some records. Surely that wouldn't interfere with the IN operator? 

 

Chuck

 


Thanks!

 

Chuck Roberts

Gilson Graphics | Composition Dept.

 

On Fri, Feb 3, 2017 at 12:37 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:

On Fri, Feb 3, 2017 at 10:12 AM, Chuck Roberts <croberts@gilsongraphics.com> wrote:

 

AND (TRIM(tbl.costcenter)  IN ('540'))

 

Can anyone shed some light on this for me?

 

 

​It works...

 

​WITH tbl (id, costcenter) AS ( VALUES (1, '500'), (2, '550') )

SELECT *

FROM tbl 

WHERE (trim(tbl.costcenter) IN ('550'));

 

So the problem resides in something you haven't show us - namely data.

 

David J.

 

 

 


Re: [NOVICE] Trouble with IN operator

От
Tom Lane
Дата:
Chuck Roberts <croberts@gilsongraphics.com> writes:
> I think this might be a data problem. If anyone else has an idea, it's
> welcome.

Well, the obvious question is, are you sure you have records that *should*
match the condition?

Plain "trim" is not very bright, it will only remove characters that are
plain ASCII spaces.  I'm wondering a bit about whether there's carriage
returns, tabs, non-breaking spaces, or other weird kinds of whitespace in
your data, which might make it so records that look like they contain
"540" don't actually match.

            regards, tom lane


Re: [NOVICE] Trouble with IN operator

От
Chuck Roberts
Дата:
Yes there is data that matches the criteria of the IN condition. When I remove the clause with the IN, I get all kinds of records that look like they match the criteria. The tbl.costcenter is a string of 3 characters, but it only contains numbers which are zero padded, like '001', '540', '900'. There should be no room for odd characters, even though users do enter this number. 

Also I tried a case-insensitive regex, and that didn't work either. Ex: 
AND (tbl.costcenter ~* '(540|001|900)')



Thanks!


On Fri, Feb 3, 2017 at 1:40 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Chuck Roberts <croberts@gilsongraphics.com> writes:
> I think this might be a data problem. If anyone else has an idea, it's
> welcome.

Well, the obvious question is, are you sure you have records that *should*
match the condition?

Plain "trim" is not very bright, it will only remove characters that are
plain ASCII spaces.  I'm wondering a bit about whether there's carriage
returns, tabs, non-breaking spaces, or other weird kinds of whitespace in
your data, which might make it so records that look like they contain
"540" don't actually match.

                        regards, tom lane

Re: [NOVICE] Trouble with IN operator

От
"David G. Johnston"
Дата:
On Friday, February 3, 2017, Chuck Roberts <croberts@gilsongraphics.com> wrote:
. And now it's critical. I'm using the psql tool
in a Windows 7 command window, version is 7.4.6.

Version 7.4 of what?
 

I wonder if I'm missing something basic, which is not even in the manuals
I've beeI've already googled to find some help but have found no info or
examples on using IN with an array of strings. So here's what I'm trying to
do:

AND (TRIM(tbl.costcenter)  IN ('540'))

I don't see an array here...
 

This results on no error, but also no records. When I take this simple
clause out, I get records. 

If you show us even one of those example records you will get much better help.

David J. 

Re: [NOVICE] Trouble with IN operator

От
Cat
Дата:
On Fri, Feb 03, 2017 at 01:48:49PM -0500, Chuck Roberts wrote:
> Yes there is data that matches the criteria of the IN condition. When I
> remove the clause with the IN, I get all kinds of records that look like
> they match the criteria. The tbl.costcenter is a string of 3 characters,
> but it only contains numbers which are zero padded, like '001', '540',
> '900'. There should be no room for odd characters, even though users do
> enter this number.
>
> Also I tried a case-insensitive regex, and that didn't work either. Ex:
> AND (tbl.costcenter ~* '(540|001|900)')

Try this:

SELECT DISTINCT char_length(tbl.costcenter), octet_length(tbl.costcenter), tbl.costcenter FROM blah WHERE
tbl.costcenterLIKE '%540%'; 

Maybe remove the LIKE comparison if it's not matching even on that.

This will helps tell you if your assumptions about the data in the DB are
correct. "should" (which you used above) is a fun-filled word. :)

--
  "A search of his car uncovered pornography, a homemade sex aid, women's
  stockings and a Jack Russell terrier."
    - http://www.dailytelegraph.com.au/news/wacky/indeed/story-e6frev20-1111118083480


Re: [NOVICE] Trouble with IN operator

От
"David G. Johnston"
Дата:
On Friday, February 3, 2017, Cat <cat@zip.com.au> wrote:
On Fri, Feb 03, 2017 at 01:48:49PM -0500, Chuck Roberts wrote:
> Yes there is data that matches the criteria of the IN condition. When I
> remove the clause with the IN, I get all kinds of records that look like
> they match the criteria. The tbl.costcenter is a string of 3 characters,
> but it only contains numbers which are zero padded, like '001', '540',
> '900'. There should be no room for odd characters, even though users do
> enter this number.
>
> Also I tried a case-insensitive regex, and that didn't work either. Ex:
> AND (tbl.costcenter ~* '(540|001|900)')


Unsurprising given that there is no concept of "case" with numbers.

David J.