Solved

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

Posted on 2016-11-22
10
67 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
[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
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 34

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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