Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-22
10
Medium Priority
?
104 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 35

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 35

Accepted Solution

by:
Pawan Kumar earned 2000 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 35

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 35

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 35

Expert Comment

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

Author Closing Comment

by:bfuchs
ID: 41899962
TY!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

877 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