cancel
Showing results for 
Search instead for 
Did you mean: 

Use string as list with IN statement

MCMartin
Participant
2,718

I have to deal with an older database. In this database no relation was used, instead the references have been stored as a comma separated list 😞

Now I need to execute a statement like this

select * from x where xid in ( 1,2,3 )

My problem is, that 1,2,3 is a value stored in one row of another table

select xid from y where yid = 1 => '1,2,3'

So how to combine this in SQL? My naive approach

select * from x where xid in ( select xid from y where yid=1 )

results in: convert 1,2,3 to numeric not possible.

Accepted Solutions (1)

Accepted Solutions (1)

If you are on V10+, you can use sa_split_list() system procedure to break comma-separated list into individual items, like this:

select * from x where xid in ( select row_value from sa_split_list (select xid from y where yid=1 ), ',')

MCMartin
Participant
0 Kudos

thanks, I have looked for a normal function but didn't remebered the SQL Anywhere specific ones.

MCMartin
Participant

@Dmitri: the idea is ok, anyway sa_split_list is not accepting the select, but it works if I use a temporary variable instead

VolkerBarth
Contributor
0 Kudos

@Martin: You will have to put the select in its own pair of brackets, as its used as a subquery expression, methinks, i.e. ...sa_split_list ((select xid from y where yid=1), ','))...

MarkCulp
Participant

@Volker: You can't use a query as a parameter to a function/procedure call so Martin's solution of using a temp variable is the way to resolve the issue.

VolkerBarth
Contributor
0 Kudos

@Mark: Thanks for correcting me - should have tested before posting:)

Answers (0)