Solved

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

Posted on 2016-11-22
10
56 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¬ificationFollowed=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 28

Expert Comment

by:Pawan Kumar
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 4

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 28

Accepted Solution

by:
Pawan Kumar 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 4

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
 
LVL 4

Author Comment

by:bfuchs
ID: 41898307
@Norie,

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

Thanks,
Ben
0
 
LVL 28

Expert Comment

by:Pawan Kumar
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 4

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 28

Expert Comment

by:Pawan Kumar
ID: 41898366
Great Ben !!
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 41899962
TY!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​

831 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