Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:21:42+00:00 2026-06-11T10:21:42+00:00

I have a table where one column has an array – but stored in

  • 0

I have a table where one column has an array – but stored in a text format:

mytable

id  ids
--  -------
1   '[3,4]'
2   '[3,5]'
3   '[3]'
etc ...

I want to find all records that have the value 5 as an array element in the ids column.

I was trying to achieve this by using the “string to array” function and removing the [ symbols with the translate function, but couldn’t find a way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:21:43+00:00Added an answer on June 11, 2026 at 10:21 am

    You can do this: http://www.sqlfiddle.com/#!1/5c148/12

    select *
    from tbl
    where translate(ids, '[]','{}')::int[] && array[5];
    

    Output:

    | ID |   IDS |
    --------------
    |  2 | [3,5] |
    

    You can also use bool_or: http://www.sqlfiddle.com/#!1/5c148/11

    with a as
    (
      select id, unnest(translate(ids, '[]','{}')::int[]) as elem
      from tbl
    )
    select id
    from a
    group by id
    having bool_or(elem = 5);
    

    To see the original elements:

    with a as
    (
      select id, unnest(translate(ids, '[]','{}')::int[]) as elem
      from tbl
    )
    select id, '[' || array_to_string(array_agg(elem), ',') || ']' as ids
    from a
    group by id
    having bool_or(elem = 5);
    

    Output:

    | ID |   IDS |
    --------------
    |  2 | [3,5] |
    

    Postgresql DDL is atomic, if it’s not late yet in your project, just structure your stringly-typed array to a real array: http://www.sqlfiddle.com/#!1/6e18c/2

    alter table tbl
    add column id_array int[];
    
    update tbl set id_array = translate(ids,'[]','{}')::int[];
    
    alter table tbl drop column ids;
    

    Query:

    select *
    from tbl
    where id_array && array[5]
    

    Output:

    | ID | ID_ARRAY |
    -----------------
    |  2 |      3,5 |
    

    You can also use contains operator: http://www.sqlfiddle.com/#!1/6e18c/6

    select *
    from tbl
    where id_array @> array[5];
    

    I prefer the && syntax though, it directly connotes intersection. It reflects that you are detecting if there’s an intersection between two sets(array is a set)

    http://www.postgresql.org/docs/8.2/static/functions-array.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of many values where one column has the WO Number,
I have a table with one column and four rows. Each tr has a
I have a table column where each row has one of three states, and
I have a table that has two datetime columns (one for start time and
I have query table copyQuery which has Soil Condition has one of the columns,
I have a database with many tables and one particular table has columns: name
I have a table inside a form. It has two columns, the first one
Screenshot mockup: http://tinypic.com/r/y2qex/5 Problem: I have a table that has 53 columns; one for
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have an HTML table with one column being email addresses; spambot issues aside,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.