1

I have a postgres query with one input parameter of type varchar.

value of that parameter is used in where clause.

Till now only single value was sent to query but now we need to send multiple values such that they can be used with IN clause.

Earlier

  value='abc'.

  where data=value.//current usage

now

  value='abc,def,ghk'.

  where data in (value)//intended usage

I tried many ways i.e. providing value as

  value='abc','def','ghk'

Or

  value="abc","def","ghk" etc.

But none is working and query is not returning any result though there are some matching data available. If I provide the values directly in IN clause, I am seeing the data.

I think I should somehow split the parameter which is comma separated string into multiple values, but I am not sure how I can do that.

Please note its Postgres DB.

2

1 Answer 1

3

You can try to split input string into an array. Something like that:

where data = ANY(string_to_array('abc,def,ghk',','))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Igor, I tried using ANY(string_to_array) as you answered however seems I don't have correct version of postgres. Though running select version() shows 15.3 but upon running ANY(string_to_array) statement I see below error. Unknown method string_to_array
@user2654241 There is no postgres version 15.3. Are you shure you are using postgres and not one of its forks?
I am using it from within Labkey's query schema browser. You are right though select version() shows 15.3 but I also got confused with it. Anyway I will try basic query and see if I can use string_to_array anyway.
There are other similar functions. Like regexp_split_to_array. Or you can write such function yourself.

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.