2

I am trying to SELECT and parse a javascript list in a postgres table column, it has no keys:

{coastal,transitional,contemporary,romantic,traditional,
industrial,modern,contemporary_eclectic,regency,mediterranean}

What SQL command get's the nth value?

I know you can get values by key like this:

SELECT {column_name}->>{key value}
FROM {table_name}

But I really want to just pull values by list-value order. Is there some syntax that I cannot find? Or do I need to transform this array into a different data type?

2 Answers 2

7

The same actually works for arrays:

{column_name}->>N

where N is the integer position of an element.

References:

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

3 Comments

I don't know, I couldn't get that to work. I just figured out the 'ish' though: it's typical syntax for JS, just doesn't start counting at 0: e.g.: {column_name}[1] is the first value in the list
@TaylorNelson it definitely is 0-base indexed.
Right, I think I had a postgres array... dumb on my part, or rather, that was the real issue, I thought I had a json.
0

Turns out I asked the wrong question--I have a postgres array, not a JSON:

I was struggling because posgres starts counting arrays at position 1, not 0--doh!

{column_name}[1] //this is the first value in the array

2 Comments

So, you don't have a JSON but a porstgresql array?
Correct, your solution is great for my question, I asked the wrong question because I thought it was a JSON array, which we have abundantly in our schema. Thanks!

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.