Skip to Main Content
  • Questions
  • IF ELSE statements within COLUMN/HAVING elements within SELECT queries

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Nelly.

Asked: February 16, 2018 - 1:30 pm UTC

Last updated: February 25, 2018 - 7:54 am UTC

Version: 12c

Viewed 1000+ times

You Asked

Hi there,
I'm trying to convert quite a few SQL queries from MS Access to PL SQL. Annoyingly a lot of them have VBA in them. Any tips on the best option to replicate this VBA in the select statement?

I could use a variable but then I'm not sure how I could do the condition on each row of the select statement. I'm guessing I would need to do sub queries of some sort... some assistance would be great.

--Example 1

SELECT Table1.KEYVAL, Table1.PRKEYVAL, Table1.REFVAL, IIf([Field1]="AGT" Or [Field2]="AGT",1,0) AS Agt, Table1.CASEDT
FROM Table1 INNER JOIN Table2 ON Table1.KEYVAL = Table2.PKEYVAL
WHERE (((Table1.CASEDT)=Date()));


--Example 2
SELECT [6_App_2].REFVAL, [6_App_2].KEYVAL, First(1) AS Email, First(0) AS Agent, Table2.CONTADD AS EmailAdd
FROM (6_App_2 INNER JOIN Table3 ON [6_App_2].KEYVAL = Table3.PKEYVAL) INNER JOIN Table2 ON Table3.KEYVAL = Table2.PKEYVAL
GROUP BY [6_App_2].REFVAL, [6_App_2].KEYVAL, Table2.CONTADD
HAVING (((Max(IIf([SBCONTYPE]="EMAIL" And ([SBAPPTYPE]="APP" Or [SBINTEREST]="APP"),1,0)))=1);


Any tips or solutions much appreciated!

and Connor said...

IIf([Field1]="AGT" Or [Field2]="AGT",1,0) AS Agt

becomes:

case when field1 = 'AGT' or field2 = 'AGT' then 1 else 0 end

and similarly for your second example

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Spot on advice.

Nelly, February 24, 2018 - 4:29 pm UTC

Timely response and with totally accurate information. Thanks!!
Connor McDonald
February 25, 2018 - 7:54 am UTC

glad we could help