This topic is locked
[SOLVED]

 IIF in Select?

12/6/2007 3:28:14 PM
ASPRunnerPro General questions
J
JOHNF777 author

I'm trying to create a calculated field in my SQL Query like:
SELECT [CustName],

[CostOfSales],

[SalesAmt],

[TranDate],

IIf([SalesAmt]=0,0,[SalesAmt]-[CostOfSales]/[SalesAmt]) AS Test

From [dbo].[Invoice1]


This work fine in MS Access.
BUT if I use it ASPRunner I get an error:

Incorrect syntax near '='
Is it possible to use some conditional statement in the Select part?
Thanks in advance.

Admin 12/6/2007

To the best of my knowledge IIF function works in MS Access only.
It won't work in ASPRunnerPro or in generated application.
You can create a Query in MS Access and save it as "qryMyQuery". After that use this query as a datasource in ASPRunnerPro.

J
JOHNF777 author 12/7/2007

I actually have a MS SQL 2000 backend. Does anyone know how to do IIF in MS SQL?
If there is an IIF similar function. I can create the view in MS SQL 2000.
Thanks.

D
dlangham 12/7/2007

Try looking up the use of CASE statements, something along the lines of the below:

CASE

WHEN [SalesAmt]=0

THEN 0

ELSE [SalesAmt]-[CostOfSales]/[SalesAmt]
J
JOHNF777 author 12/10/2007

Thanks, Dale. I tried it in ASPRunner SQL Edit and MSSQL View and CASE does not work.

<img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=24053&image=1&table=forumreplies' class='bbc_emoticon' alt=':(' />
Still looking for solution.
Thanks,

John

Admin 12/10/2007

John,
I suggest to proceed to SQL Server Books to find a correct syntax for CASE statement. It comes with SQL Server.
Specifically you missing END

CASE

WHEN [SalesAmt]=0

THEN 0

ELSE [SalesAmt]-[CostOfSales]/[SalesAmt]

END
D
dlangham 12/10/2007

John,
Please go to the following url:
http://msdn2.microsoft.com/en-us/library/ms181765.aspx

J
JOHNF777 author 12/11/2007

Thanks, Dale that's a good site for future reference.
I found my issue and solution though......
I have to create my MS SQL VIEW using Query Analyzer (at least for MS SQL2000).

Here's a sample code:
CREATE VIEW MyView2 AS

SELECT

TDate,

SalesAmt,

CostOfSales,

CASE WHEN SalesAmt=0 THEN 0

ELSE SalesAmt/CostOfSales End as GP

FROM TableInvoices

After creating the view I can modify it after. The only thing is you'll get the error: "QUERY DESIGNER DOES NOT SUPPORT THE CASE SQL CONSTRUCT" though you can still save the View. You can't view the query in Query Designer but it save the code.
There's also another solution for the divide by 0, you can also use the following sample that you can use within ASPRunner:

Select [SalesAmt],

[CostOfSales],

[FYear],

(([SalesAmt] - [SalesCost])/NullIf([SalesAmt],0)) AS GP_Percent

From [dbo].[TableInvoices]


Hope this helps someone.

D
dlangham 12/11/2007

Glad you got it sorted in the long run.