I am trying to convert the Comma separated string into an integer array (integer[]) to use in Where clause.
I have tried cast, ::Int
which didn't work. Appreciate your input
Example
Table A | Table B
ID | Set_id
2 | 14,16,17
1 | 15,19,20
3 | 21
My Query:
Select *
from Table a, table b
where a.id in b.set_id
The PostgreSQL TO_NUMBER() function converts a character string to a numeric value.
Array Type. PostgreSQL gives the opportunity to define a column of a table as a variable length single or multidimensional array. Arrays of any built-in or user-defined base type, enum type, or composite type can be created. We will focus on one data type in particular, the Array of text, text[].
PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created.
PostgreSQL CONVERSION is used to convert between character set encoding. CREATE A CAST defines a new cast on how to convert between two data types. Cast can be EXPLICITLY or IMPLICIT . The behavior is similar to SQL Server's casting, but in PostgreSQL, you can also create your own casts to change the default behavior.
You need to convert the string to a proper integer array if you want to use that for a join condition.
Select *
from Table a
join table b on a.id = any(string_to_array(b.set_id, ',')::int[]);
But a much better solution would be to properly normalize your tables (or at least stores those IDs in an integer array, not a varchar column)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With