1

Hope I read it correctly. Subscripting can be used in text type, like select name[1] will get the first character of the text column name? Now this will not work. Probably need a function? when I do it in psql.

ERROR: 42804: cannot subscript type text because it does not support subscripting
LOCATION: transformContainerSubscripts, parse_node.c:275

So, how to make subscripting available for text data type?

https://www.postgresql.org/docs/release/14.0/

Subscripting can now be applied to any data type for which it is a useful notation, not only arrays. In this release, the jsonb and hstore types have gained subscripting operators.

2
  • 3
    The release notes mention three types: arrays, hstore and jsonb - text is neither of them. So, no, you did not read it correctly. Commented Mar 8, 2022 at 15:14
  • 1
    for subscripting text data you can use the function substring() see the manual Commented Mar 8, 2022 at 15:23

1 Answer 1

1

I got the same error below:

ERROR: cannot subscript type text because it does not support subscripting

When I tried to get the 2nd character b as shown below:

SELECT ('abc'::TEXT)[2]; -- Error

So, I created an array with these ways below, then I could get the 2nd character b without error. *My answer explains how to declare and use an array in detail:

SELECT (ARRAY['a','b','c']::TEXT[])[2]; -- b
SELECT ('{a,b,c}'::TEXT[])[2]; -- b
SELECT (string_to_array('a,b,c', ',')::TEXT[])[2]; -- b
Sign up to request clarification or add additional context in comments.

Comments

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.