Solved

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

Posted on 2016-11-22
10
49 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

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…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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