Oracle SQL problem with decode

Status
Not open for further replies.

vanguard

Wise, Aged Ars Veteran
194
Friends,

I'm trying to write a select statement that ends like this:

Code:
and sc.customer_name in (DECODE(shipto,'0',sc.customer_name,shipto));

The trouble is that shipto contains a comma delimted list. I think that oracle is expanding it so that it works out to be like this:

Code:
and sc.customer_name in (DECODE shipto,'0',sc.customer_name,'XYZ','ABC'));

So I want it to match against the variable if the value passed isn't 0 but what's happening is the decode function is looking at it as another if/else block.

Any ideas?

Vanguard
 

Dandy Andy

Ars Tribunus Militum
2,458
I'm not quite clear on what you want - am I right in thinking the following?

"Find the all rows where 'sc.customer_name' is contained within 'shipto', unless 'shipto' is '0', in which case return all customers."

The SQL to do this would be:

WHERE shipto = '0'
OR (shipto <> '0' AND INSTR(shipto,sc.customer_name) <> 0)

Or have I missed the point?

:confused:
 
Status
Not open for further replies.