Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Oracle doesn't have an <code>IF</code> for SQL. Use <code>CASE</code> instead - it can act like a C/C#/Java/etc <code>switch - case</code> as you have it in your query, and it can also act like an <code>if</code>.</p> <p>Also, as mentioned above:</p> <ul> <li>You're stuck with returning a constant number of columns unless you use dynamic SQL</li> <li>In Oracle, <code>''</code> is treated as NULL so instead of <code>= ''</code> use <code>IS NULL</code>.</li> </ul> <p>If you don't want to go to Dynamic SQL, you could add a "type" column to your select list and then null out any inapplicable values. Your downstream logic could pick the values to use (or ignore) based on the type. Here's an example:</p> <pre><code>SELECT mi.date, CASE WHEN inAttribute IS NULL THEN 'inAttrNull' ELSE 'inAttrNotNull' END AS RecordType, CASE WHEN inAttribute IS NULL THEN mi.compprice END AS compprice, CASE WHEN inAttribute IS NULL THEN mi.compspread END AS compspread, CASE WHEN inAttribute IS NULL THEN mi.price END AS price, CASE WHEN inAttribute IS NULL THEN mi.spread END AS spread, CASE WHEN inAttribute IS NULL THEN mi.run END AS run, CASE inAttribute WHEN 'CP' THEN mi.compprice WHEN 'CS' THEN mi.compspread WHEN 'MP' THEN mi.price WHEN 'MS' THEN mi.spread WHEN 'R' THEN mi.run END AS SpecialValue FROM userValueTable mi ... and your WHERE and ORDER BY clauses </code></pre> <p>You'll get a result set something like this:</p> <pre><code>RecordType compprice compspread price spread run specialvalue ------------- ---------- ---------- ---------- ---------- ---------- ------------ inAttrNotNull (null) (null) (null) (null) (null) 1234.56 inAttrNull 111.11 222.22 333.33 444.44 555.55 (null) </code></pre> <p>I know this isn't what you wanted, but it may be something you can work with as an alternative.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload