Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 13th, 2005, 03:02 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.


Reply With Quote
  #2 (permalink)  
Old April 13th, 2005, 03:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

use CASE instead
CASE WHEN 5>4 THEN 'Yes' ELSE 'No' END
Reply With Quote
  #3 (permalink)  
Old April 13th, 2005, 03:36 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Forgot the SELECT at the start
SELECT CASE WHEN 5>4 THEN 'Yes' ELSE 'No' END
Reply With Quote
  #4 (permalink)  
Old April 13th, 2005, 04:01 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?




Reply With Quote
  #5 (permalink)  
Old April 13th, 2005, 04:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
  #6 (permalink)  
Old April 13th, 2005, 04:45 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.


Reply With Quote
  #7 (permalink)  
Old April 13th, 2005, 05:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Isn't that just the same as:
select sum(CASE WHEN trn.amountspend = 0 THEN trn.budgetamount ELSE trn.amountspend END) as budgetamount, ...

Reply With Quote
  #8 (permalink)  
Old April 13th, 2005, 05:28 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

GREAT!!! i got exactly the same results as for original ms-access. thanks a lot.



Reply With Quote
  #9 (permalink)  
Old December 6th, 2009, 10:54 AM
Registered User
 
Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #10 (permalink)  
Old December 6th, 2009, 10:46 PM
Friend of Wrox
Points: 1,536, Level: 15
Points: 1,536, Level: 15 Points: 1,536, Level: 15 Points: 1,536, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

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..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with IIf function rashi Access VBA 3 February 15th, 2008 10:07 AM
IIf umeshtheone Beginning VB 6 3 June 21st, 2007 12:24 AM
IIf Function - Multiple conditions carrie09 Access 2 June 13th, 2007 10:27 AM
EVAL becomes failed, with IIF and Replace Function AbrarNazeer ASP.NET 2.0 Professional 0 July 6th, 2006 06:53 AM
Is there an IIF Function in SQL Server? treadmill Classic ASP Professional 4 January 17th, 2005 06:57 AM



All times are GMT -4. The time now is 05:12 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.