 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 13th, 2005, 03:02 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
iif function
Does SQL Server 2000 support iif(logical expression,"","") function.
i tried this query just to know whether it supports this function.
select iif(5>4,"Yes","No")
and got the error Incorrect Syntax near ">"
what's the solution. this isn't my main query. main query is quite big. but just to know how to make access query using iif() compatible with sql server query i tried this small query.
pls help!!
thanks.
|
|

April 13th, 2005, 03:33 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
use CASE instead
CASE WHEN 5>4 THEN 'Yes' ELSE 'No' END
|
|

April 13th, 2005, 03:36 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Forgot the SELECT at the start
SELECT CASE WHEN 5>4 THEN 'Yes' ELSE 'No' END
|
|

April 13th, 2005, 04:01 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks. But then the problem is that i am refering to some field in the table with relational operator instead of simple 5>4. when the field is used then it needs to be in group by or coupled with aggregate function. if that's done i do not get the same result as before as it changes the whole logic of the query. i already have a group by clause in the main query. after adding one more column in it, it would give wrong result.what's the possible solution for this?
|
|

April 13th, 2005, 04:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
You'll have to give more details of your query then, because the bottom line is that SQL Server does not support IIF so if you want to do something like IIF you'll have to say EXACTLY what you want.
|
|

April 13th, 2005, 04:45 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My original ms-access query goes like this
select sum(iif(trn.amountspend = 0, trn.budgetamount, trn.amountspend)) as budgetamount,
trneventheader.regionid , mstrg.region,count(trn.eventid) as NoOfEvents,sum(trn.EndLeadGenerated) as EndLead,
(select amountallocated from mstlaapspend where mstlaapspend.regionid=mstrg.regionid) as Amountlocated
from mstregion mstrg, mstlaapspend mstlp inner join trneventheader on mstlp.regionid=trneventheader.regionid,
trnactivity trn where trn.eventid=trneventheader.eventid and mstlp.regionid=mstrg.regionid and
mstrg.regionid=trneventheader.regionid and trn.dateofactivity > '04/01/2004' and trn.approvedstatus=1
group by trneventheader.regionid, mstrg.regionid, mstrg.region
order by mstrg.region
----
In this what used to happen is that if trn.amountspend=0 then sum of budgetamount used to get calculated else sum of amountspend used to get calculated. if we want to do the same in sql server, as iif isn't supported sql server does not consider trn.amountspend as an attribute just for the sake of boolean expression but considers it as a field which should be shown in recordset. hence it puts the message as aggregate function or group by clause should be used for that field.
hence the problem. i hope u got what i wanted to say.
thanks.
|
|

April 13th, 2005, 05:14 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Isn't that just the same as:
select sum(CASE WHEN trn.amountspend = 0 THEN trn.budgetamount ELSE trn.amountspend END) as budgetamount, ...

|
|

April 13th, 2005, 05:28 AM
|
|
Registered User
|
|
Join Date: Apr 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
GREAT!!! i got exactly the same results as for original ms-access. thanks a lot.
|
|

December 6th, 2009, 10:54 AM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Plz help me anyone...!!!!!!!!!
my query is in sql server 2005..
this is as follows:-
stat = "Select sum(CASE WHEN AmtCrDr = 0 THEN Amount ELSE -Amount END from EtAmtTransMast where Ledgerid = c.partyid)as [Bal] From PersonalDetails c where type=" & PersonType.Customers
and the error is:-
Incorrect syntax near the keyword 'from'.
i dont know what is the prob with this... plz plz help me......
thanks
|
|

December 6th, 2009, 10:46 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
You have two FROM clauses and two WHERE clauses. If you want to reference two tables, you must do some sort of JOIN between the two tables (INNER, OUTER, whatever) with an ON clause and any WHERE clauses would follow that.
__________________
--Jeff Moden
Last edited by Jeff Moden; December 6th, 2009 at 10:49 PM..
|
|
 |