0

I have integer Array like {10,13,20}

I have to pass this array in IN condition like

SELECT count(*) FROM table WHERE id IN (10,20,30)

I tried:

SELECT count(*) FROM table WHERE id IN(array_to_string(_array,','))

but it's throwing the error casting operator does not exist: bigint = text

Thanks in advance

4
  • What is _array? What are you trying to achieve by using array_to_string? Commented Dec 29, 2021 at 14:05
  • _array is {10,20,30} Commented Dec 30, 2021 at 3:56
  • As an array value (from where?)? Or as a literal? Commented Dec 30, 2021 at 5:27
  • Array pass to stored procedure Commented Dec 30, 2021 at 5:28

2 Answers 2

1

you can use below query:

Demo

SELECT count(*) FROM test WHERE id = any(array[10,20,30]::int[]);

Sign up to request clarification or add additional context in comments.

6 Comments

I passed array in {10,20,30} this format.
I have array which in this format : {10,20,30} Not in this format : array[10,20,30]
Not diffrence between array[10,20,30]::int[] and {10,20,30}. You can use both of them dbfiddle.uk/…
You are right but when pass array in stored procedure its show "{10,20,30}" and throw the error
@KrunalPandya Please edit your question to show the entire stored procedure definition. Is _array really of type int[] (or bigint[])?
|
0

Use string_to_array instead of array_to_string. Cast the resulting array to bigint[] as the type of id is bigint. Here it is, easy to be parameterized:

SELECT count(*) FROM users u
 WHERE id = any(string_to_array('10,20,30',',')::bigint[]);

After your clarification that _array is an argument or variable of type bigint[] then simply:

SELECT count(*) FROM users u WHERE id = any(_array);

4 Comments

"the type of id is bigint." - uh, how do you know? The OP didn't share their table definition.
@Bergi The OP shared the error message 'operator does not exist: bigint = text'
it's like this SELECT count(*) FROM users u WHERE id = any(string_to_array({10,20,30},',')::bigint[]); Its work ?
No, string_to_array expects its first argument to be text, no curly braces are needed. string_to_array('10,20,30',','). Pls. see updated answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.