If Statements in Microsoft Query

bluelabel

Board Regular
Joined
Nov 27, 2008
Messages
76
Hi,

Could someone please help me with the correct way to use an If statement to pull data into Excel via Microsoft Query (ODBC connection)

I need if a certain column is a number 2, then convert the result to a negative, if not then normal result

SQL:
SELECT invoicedetailproduct.internaldocnum, IIF(invoiceheader.doctype=2, invoicedetailproduct.taxableamt*-1, invoicedetailproduct.taxableamt)
FROM admin.invoicedetailproduct invoicedetailproduct, admin.invoiceheader invoiceheader
WHERE invoiceheader.internaldocnum = invoicedetailproduct.internaldocnum

No matter which way I do the formula I end up with a Syntax Error, so I'm obviously missing something.

Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
where are you pulling the data from what is the source database and type
 
Upvote 0
what is the name of the accounting system it may well have sql but depends on vintage and vendor I believe iif is a recent addition to sql 2012 and behaves in the same way as CASE again you may find this variant of sql does not support CASE
 
Upvote 0
Your SQL looks correct. Maybe missing spaces, before FROM and WHERE.
SQL:
SELECT invoicedetailproduct.internaldocnum, IIF(invoiceheader.doctype=2, invoicedetailproduct.taxableamt*-1, invoicedetailproduct.taxableamt)
 FROM admin.invoicedetailproduct invoicedetailproduct, admin.invoiceheader invoiceheader
 WHERE invoiceheader.internaldocnum = invoicedetailproduct.internaldocnum
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top