Solved

How to place a condition in a filter criteria in t-sql (#2)?

Posted on 2016-11-22
10
20 Views
Last Modified: 2016-11-23
Hi Experts,

I need some modification to the following posts answer.
https://www.experts-exchange.com/questions/28984589/How-to-place-a-condition-in-a-filter-criteria-in-t-sql.html?anchor=a41896938&notificationFollowed=179661574#a41896938
I want to change the following code
SELECT * FROM Wordings
WHERE 
	  ISNULL( DateFields , '' ) >=  CASE WHEN Wording LIKE '%My Wording%' THEN '1/1/16' ELSE ISNULL(DateFields ,'') END

Open in new window

that in case the wording is found then instead of selecting where datefields >= '1/1/16', I want to put the following "EmployeeID in (select id from employeestbl where datefields >= '1/1/16')"
PS. The reason for this change is that the date field is in another table.

Thanks in advance.
0
Comment
Question by:bfuchs
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41898258
Try..

SELECT * FROM Wordings w
INNER JOIN employeestbl e ON e.ID = W.EmployeeID
WHERE ISNULL( w.DateFields , '' ) >=  CASE WHEN w.Wording LIKE '%My Wording%' THEN '1/1/16' ELSE ISNULL(w.DateFields ,'') END

--

Open in new window

0
 
LVL 3

Author Comment

by:bfuchs
ID: 41898269
Hi Pawan,

The story here is as follows..

As described in the first post, this criteria is being build dynamically, meaning the record source is pre-defined and just the filter keeps changing according to what users have selected.

Therefore I would need a solution that keeps the select * from Wordings intact and just changes the where clause to insert the filter in case of that wording..

Let me know if its clear.

Thanks,
Ben
0
 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
ID: 41898276
Something like...

SELECT * FROM Wordings w
WHERE EmployeeID IN 
	  (
	  
		SELECT Id from employeestbl 
		WHERE DateFields >= CASE WHEN w.Wording LIKE '%My Wording%' THEN '1/1/16' ELSE ISNULL(DateFields ,'') END
	  
	  ) 

Open in new window

0
 
LVL 33

Expert Comment

by:Norie
ID: 41898293
Ben

Do you want to apply the condition in the code that's creating the SQL rather than in the SQL itself?
0
 
LVL 3

Author Comment

by:bfuchs
ID: 41898301
Ok thats on the way..

I just have an issue with this regarding performance, as it will run the statement where employeeid in (select id .. for every row, while its only necessary for those who have that wording.

How about we do something like where wording not like "%My Wording%" or id in (select id from employeestbl where datefields >= '1/1/16')?

Thanks,
Ben
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Author Comment

by:bfuchs
ID: 41898307
@Norie,

Not sure how's that possible as this depends on each record?

Thanks,
Ben
0
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41898309
Try.. I hope my last comment did not work.

SELECT * FROM Wordings
WHERE 
id IN ( CASE WHEN Wording NOT LIKE '%My Wording%' THEN (select id from employeestbl where datefields >= '1/1/16') ELSE (SELECT Id FROM Wordings) END )

Open in new window

0
 
LVL 3

Author Comment

by:bfuchs
ID: 41898365
Hi Experts,

At the moment looks like the following will do whats needed in our app.

select * from wordings 
	  where wording  like '%My Wording%' and id in (select id from employeestbl where employeestatusdate >= '1/1/06')

Open in new window


However will wait with finalizing until I integrate with the app.

Thanks,
Ben
0
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41898366
Great Ben !!
0
 
LVL 3

Author Closing Comment

by:bfuchs
ID: 41899962
TY!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now