2

I have a field:

dtype ==> character varying(3)[]

... but it's an array. So I have for example:

ID | name | dtype
1  | one  | {'D10', 'D20', 'D30'}
2  | sam  | {'D20'}
3  | jax  | {'D10', 'D20'}
4  | pam  | {'D10', 'D30'}
5  | pot  | {'D10'}

I want to be able to do something like this:

select * from table where dtype in ('D20', 'D30')

This syntax doesnt work, but the goal is to then return fields 1,2,3,4 but not 5.

Is this possible?

7
  • 2
    I would say by using 'overlap' operator &&... postgresql.org/docs/9.1/static/functions-array.html Commented May 23, 2014 at 9:16
  • Your table definition and the contents don't match. character varying(3) is not an array. It's a varchar column with a max. length of 3. But if that were true, you wouldn't be able to store something like {'D10', 'D20', 'D30'}. So I guess your column is actually character varying[]? Can you do a \d table_name and show us the complete table definition? Commented May 23, 2014 at 9:26
  • if dtype is just a normal varchar, postgresql won't know you have placed some data in it that looks like some form of array. Can you use real postgresql arrays ? Commented May 23, 2014 at 9:26
  • So far "unnest" seems to be the best way to go about it, but I was hoping there'd be a better solution. Commented May 23, 2014 at 9:29
  • 2
    you can use the contains operator as Pavel has suggested: dtype && array['D20', 'D30']::varchar[] Commented May 23, 2014 at 9:53

1 Answer 1

1

Use the && operator as shown in the PostgreSQL manual under "array operators".

select * from table where dtype && ARRAY['D20', 'D30']
Sign up to request clarification or add additional context in comments.

Comments

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.