views:

164

answers:

3

I have a rather simple PL/SQL query

(...)
DECODE(upper(Table.Column), 'SOMETHING', '---', TABLE2.COLUMN2) as VALUE
(...)

How can I apply the WHERE clause using above statement, because when I use something like:

WHERE upper(VALUE) like 'SOMETHING'

it gives me an empty recordset (and I am absolutely positive that there are records which fulfill those requirements).

I don't know if that matters, but I am calling this query from within Excel workbook, using VBA and CreateObject("OracleInProcServer.XOraSession").

+1  A: 

The easiest solution is to copy and paste.

WHERE upper(DECODE(upper(Table.Column), 'SOMETHING', '---', TABLE2.COLUMN2)) like 'SOMETHING'

SOMETHING is not a good search pattern for LIKE though.

ammoQ
It worked:) Thank You very much:) 'Something' was just put here to make this readable.
zeroDivisible
+1  A: 

The output of the DECODE function would be either '---' or table2.column2.

This decode is equivalent to:

CASE upper(Table.Column) = 'SOMETHING' THEN '---' ELSE TABLE2.COLUMN2 END

I'm guessing you want to filter the rows that have upper(TABLE.Column) = 'SOMETHING', in that case you would use :

WHERE upper(VALUE) = '---'
Vincent Malgrat
+5  A: 

The problem with the accepted solution is that you have to write out the DECODE expression twice. This can be a maintenance issue, because it opens up the possibility that the two instances of the expression will diverge at some point.

Another way to do it is to nest queries so you can use the result of the DECODE directly in your predicate.

SELECT * FROM (
  SELECT DECODE(upper(Table.Column), 'SOMETHING', '---', TABLE2.COLUMN2) as VALUE
    FROM ...
  )
  WHERE UPPER(value) LIKE 'X%'
Dave Costa
You may be are right with this one but...I am PL/SQL newbie with very small knowledge about SQL. I just chosen simpler solution.
zeroDivisible
Absolutely correct, I thought of that too. But for a SQL newbie, it might just be a bit too much. Anyway, +1 for your solution.
ammoQ