?
Solved

Select records based on boolean parameter

Posted on 2013-02-04
8
Medium Priority
?
257 Views
Last Modified: 2013-02-04
I have a query and I'm trying to add code so that if the parameter @Client is true then only select records with a Class of either 2 or 6.  Else If @Client is false then select All classes.  Would you show me what I'm doing wrong?


select	workdate, empId, L.LastName + ', ' + L.FirstName AS Name, ManagerUID, 
convert(varchar(5), dateadd(second, Total_WorkDate_Units, '0:00:00'),108) as HrsMin,
		CASE WHEN @Clients = -1 then L.Class IN (2, 6) END  
from	Employee_Work_Units_Summary s inner join dbo.EmployeeList L on s.empId = L.EmployeeId
where	Total_WorkDate_Units >  32400 AND (L.Suspend = 0)  and   (ManagerUID = ISNULL(NULLIF (@Supervisor, 0), ManagerUID))  AND (@Employee =0 or L.EmployeeId =@Employee) 
Order by workdate, Name]

Open in new window

0
Comment
Question by:BobRosas
[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 40

Expert Comment

by:Kyle Abrahams
ID: 38852591
CASE WHEN @Clients = -1 then L.Class IN (2, 6) END  

should be:

Case WHEN @Clients = 1 then L.Class in (2,6) else L.Class in (select distinct class from <table>) END
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 1000 total points
ID: 38852593
SELECT blah, blah, blah
FROM YourTable
WHERE ( (l.class IN (2, 6) AND @client = True) OR @client = False) )
0
 

Author Comment

by:BobRosas
ID: 38852826
Thank you both for your quick response.  I'm still trying to get the code to work.  

ged325
I tried the following but I can't even get it to compile.  I don't know what I'm missing...

select	workdate, empId, L.LastName + ', ' + L.FirstName AS Name, ManagerUID, 
convert(varchar(5), dateadd(second, Total_WorkDate_Units, '0:00:00'),108) as HrsMin,
Case WHEN @Clients = 1 then L.Class in (2,6) else L.Class in (select distinct class from dbo.EmployeeList) END 
from	Employee_Work_Units_Summary s inner join dbo.EmployeeList L on s.empId = L.EmployeeId
where	Total_WorkDate_Units >  32400 AND (L.Suspend = 0)  and   (ManagerUID = ISNULL(NULLIF (@Supervisor, 0), 
ManagerUID))  AND (@Employee =0 or L.EmployeeId =@Employee) 
Order by workdate, Name

Open in new window



jim horn...
I tried this ...
AND( (L.class IN (2, 6) AND @Clients = -1) OR @Clients = 0)   

Open in new window

because I have @Clients as type bit and I got an invalid error using true.  But even with that change the result is not what I expect.  
EX:
Client     Class
101              5
102              3
103             4
104              1
105              2
106               6
107               4
108                3
109                 2
110                9

For the above data, when @Clients is true(-1) I want only 2's and 6's so I should have 3 records.
When @Clients = False (0) I want all records so I'm expecting 10 records.  That's why I'm trying to not filter by Class if @Clients is False.  Maybe that's not possilbe?
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 38852893
clients  is true = 1 . . . not -1

AND( (L.class IN (2, 6) AND @Clients = 1) OR @Clients = 0)
0
 

Author Comment

by:BobRosas
ID: 38853011
ged325
Thank you for your input.  I made that change but my data results did not change.  Should they?
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 1000 total points
ID: 38853034
try the following:

select      workdate, empId, L.LastName + ', ' + L.FirstName AS Name, ManagerUID,
convert(varchar(5), dateadd(second, Total_WorkDate_Units, '0:00:00'),108) as HrsMin
            from      Employee_Work_Units_Summary s inner join dbo.EmployeeList L on s.empId = L.EmployeeId
where      Total_WorkDate_Units >  32400 AND (L.Suspend = 0)  and   (ManagerUID = ISNULL(NULLIF (@Supervisor, 0), ManagerUID))  AND (@Employee =0 or L.EmployeeId =@Employee)
AND( (L.class IN (2, 6) AND @Clients = 1) OR @Clients = 0)

Order by workdate, L.LastName + ', ' + L.FirstName
0
 

Author Comment

by:BobRosas
ID: 38853093
Thanks again!  I copied and pasted your code right in.  It compiles and runs but if I enter 1 (true) I don't get any results (I'm expecting 1 record).  If I enter 0 (false)  it excludes class 2 and 6 and I would like to include all classes for false.  I can't figure out what I'm missing but I do appreciate your help.
0
 

Author Comment

by:BobRosas
ID: 38853186
I found it!  I was comparing a report that's been working for a long time to a newly created Report Services report (which I thought was working).  But a circumstance that I didn't code for in report services just happen to be in my test data set and I didn't see it.  Once I ran and compared other data it looked good.  I apologize and thank you both so much. I will increase points and split them.  EE is awesome!
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

770 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