Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

From Acess IIF to SQL Server CASE, anyone, please?

Posted on 2009-04-06
8
Medium Priority
?
314 Views
Last Modified: 2012-05-06
Hello again experts.

can you please help to convert this IIF statement that I am current using in MS Access to MS SQL Server?

IIF(Inactive_Has_Clients,2,1)

Thanks for your assistance
0
Comment
Question by:simflex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:MNelson831
ID: 24078595
IIF(Inactive_Has_Clients,2,1)

Select Case when Inactive_Has_Clients then 2 else 1 end
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24078604
Try this one out:

SELECT CASE WHEN Inactive_Has_Clients = 2 THEN 1 ELSE NULL END AS urcolumnname
FROM urtable
0
 

Author Comment

by:simflex
ID: 24078689
Ok, So I wasn't really that crazy because I tried both and none worked.

None worked meaning that normally, I get the form field populated with values from the IIF() function but it didn't work when I converted to sql an hour ago.

I will try each of them again and post back.

Many thanks to both of you for the prompt responses.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 15

Expert Comment

by:MNelson831
ID: 24078725
Is Inactive_Has_Clients a boolean expression?  It has to be if your IIF statement works.  The code you posted reads:

If the value of Inactive_Has_Clients is true then return 2 otherwise return 1

Is that what you intended?
0
 

Author Comment

by:simflex
ID: 24078912
Yes, Inactive_Has_Clients is a boolean (bit) data type in sql server
and yes
If the value of Inactive_Has_Clients is true then return 2 otherwise return 1

Those are correct but here is the error I am getting:

An expression of non-boolean type specified in a context where a condition is expected, near 'then'

I have no clue what that means. Isn't bit data type a bolean?
0
 
LVL 15

Accepted Solution

by:
MNelson831 earned 200 total points
ID: 24079024
No it isn't.  Try this:

Select Case when  Inactive_Has_Clients <> 0 Then 2 Else 1 End
0
 

Author Comment

by:simflex
ID: 24079119
still getting this error:

[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'when'

Here is more code maybe my fault as I didn't give enough code. I didn't anticipate it being a challenge; that's why I gave little code.

Here is the entire code and lots of thanks for your help.


SQL	= "select commonname, Inactive_Has_Clients from tblUsers" _
        & " where Code = 'Mgr' " _
        & " and ( active=True OR Inactive_Has_Clients = True ) " _
        & " order by IIF(Inactive_Has_Clients,2,1), commonname"

Open in new window

0
 

Author Comment

by:simflex
ID: 24080105
Ok, I got it working.

You were right on track with this:

Select Case when  Inactive_Has_Clients <> 0 Then 2 Else 1 End

I made some slight changes to this:

(Case when  Inactive_Has_Clients <> 0 Then 2 Else 1 End)

and it is working now.
It is amazing how brackets could make such a difference.

Thanks for all your help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question