Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Nth element of an array that returns from "string_to_array()" function

I am searching for a way to access to the Nth element of an array which is a result of string_to_array() function in PostgreSQL. For example,

Assume that a cell contains the string value: "A simple example" . If I use string_to_array() function, I will have an array of three strings as ('A','simple','example'). Now, without storing (I mean, on the fly) I want to access the 2nd element of this array, which is 'simple'.

During my googling, I saw an example to access the last element of the array but this barely solved my problem.

Is there a way to do this?

like image 954
iso_9001_ Avatar asked Aug 06 '12 15:08

iso_9001_


People also ask

Can be used accessing the nth element of an array?

In case of an array, the address of the n th element can be computed since an array is allocated in contiguous memory locations. Thus, an element of an array can be accessed in O(1) time. In the case of a linked list, to access the n th element, you will need to traverse the linked list to reach the n th element.

What is nth element in Javascript?

The :nth-child() pseudo-class takes a single argument that describes which elements should be matched. Note that element indices are 1-based when using :nth-child() . You can narrow things down by only matching an element of a specific type, e.g. the second positioned div element. index.js. Copied!


1 Answers

select (string_to_array('1,2,3,4',','))[2]; 
like image 101
devanand Avatar answered Oct 23 '22 02:10

devanand