Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write an array literal in SQL?

Is it possible to use an array literal in Oracle, such as:

SELECT [1,2,3] FROM dual
# [1,2,3] -- int[]

Or:

SELECT ([1,2,3])[0] FROM dual
# 1 -- int

Or what’s the simplest (i.e., possible not in PL/SQL?) way to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

if you wanta flexible list, y an use a type like this

create type t_inttable as table of int
/
create table tab_mydata (
  id         number(10),
  intlist   t_inttable
)
nested table intlist store as ntab_intlist
insert into tab_mydata values(
  1, 
  t_inttable(1,2,3)
)
/
select * from tab_mydata
/
ID | INTLIST
-: | :------
select t.id, x.column_value from tab_mydata t, table(intlist) x
/
ID | COLUMN_VALUE
-: | -----------:
 1 |            1
 1 |            2
 1 |            3
select t.id, x.column_value from tab_mydata t, table(intlist) x
where x.column_value like 3
/
ID | COLUMN_VALUE
-: | -----------:
 1 |            3

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading