How to correct an IIF statement in SQL Server 2005?

I am developing a C# Web site application. I use VS2010 and SQL Server 2005.

Why does the following SQL statement give me an error as follows:
Incorrect syntax near '='.

The offending statement is:

IIf(sign_='-',CDbl(tbl_SMR_OI_DRS.amount*(-1)/100),CDbl(amount)/100),

----------------------------------------------------------------------------------------------------
The entire SQL statement is as follows:
SELECT tbl_SMR_OI_DRS.bankNum, tbl_SMR_OI_DRS.officeNum, checknum, tbl_SMR_OI_DRS.refnum,
tbl_SMR_OI_DRS.transDTMnth + '/' + tbl_SMR_OI_DRS.transDTday + '/' + tbl_SMR_OI_DRS.transDtYR,
tbl_SMR_OI_DRS.tranType,
tbl_SMR_OI_DRS.processDTMnth + '/' + tbl_SMR_OI_DRS.processDtDay + '/' + tbl_SMR_OI_DRS.processDTYear,
tbl_SMR_OI_DRS.pending, tbl_SMR_OI_DRS.age*1, tbl_SMR_OI_DRS.cr_db, tbl_SMR_OI_DRS.descr,
IIf(sign_='-',CDbl(tbl_SMR_OI_DRS.amount*(-1)/100),CDbl(amount)/100),
tbl_SMR_OI_DRS.userID,
tbl_SMR_OI_DRS.auditDate, ' ', ' ', tbl_SMR_Banks.rptID, tbl_SMR_Banks.[REPORT NAME]
FROM tbl_SMR_OI_DRS INNER JOIN tbl_SMR_Banks ON tbl_SMR_OI_DRS.bankNum=tbl_SMR_Banks.[Bank Code];
zimmer9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
probably a result in null values
and ther is no try/catch statement to display the error

here is a definition of IIF statememnt
http://technet.microsoft.com/en-us/library/hh213574.aspx
0
DcpKingCommented:
I have no idea of your table layouts, but:

1. What table does checknum come from - you don't give it a table.
2. What table does sign_ come from, and is it really called "sign_" or did you miss
      something from the end of it?
3. You have two fields set to a single space. If you really mean an empty string use two ticks next to each other.
4. SQL allows you to alias your table names, so that, in my very humble opinion, this might be a little more readable:
	SELECT	drs.bankNum, 
			drs.officeNum, 
			checknum, 
			drs.refnum,
			drs.transDTMnth + '/' + drs.transDTday + '/' + drs.transDtYR,
			tbl_SMR_OI_DRS.tranType,
			drs.processDTMnth + '/' + drs.processDtDay + '/' + drs.processDTYear,
			drs.pending, 
			drs.age*1, 
			drs.cr_db, 
			drs.descr,
			IIf(	sign_ = '-',	
					CDbl(drs.amount * (-1)/100),	
					CDbl(drs.amount) / 100
				),
			drs.userID,
			drs.auditDate, 
			' ', 
			' ', 
			bnk.rptID, 
			bnk.[REPORT NAME]
	FROM tbl_SMR_OI_DRS         drs
	INNER JOIN tbl_SMR_Banks  bnk 		
	ON drs.bankNum = bnk.[Bank Code]; 

Open in new window

In addition, I believe that you are converting here from Access: there is no CDbl in T-SQL. Instead, your IIF statement should look like this:

			IIf(	sign_ = '-',	
					cast(drs.amount as float) * -0.01),	
					cast(drs.amount as float) / 100.0
				),

Open in new window


Finally, if you're not using SQL Server 2012 then IIF doesn't exist, so use this:
			case
					when sign_ = '-', then cast(drs.amount as float) * -0.01),	
					else cast(drs.amount as float) / 100.0
			end,

Open in new window


Hope this all helps


Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zimmer9Author Commented:
CASE when sign_ = '-' then cast(tbl_SMR_OI_DRS.amount as float) * -0.01 else cast(tbl_SMR_OI_DRS.amount as float) / 100.0 END,
0
DcpKingCommented:
That's it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.