Solved

How to place a condition in a filter criteria in t-sql?

Posted on 2016-11-21
12
90 Views
Last Modified: 2016-11-22
Hi Experts,

I'm building a dynamic sql string and looking for the following.

Lets say there are two fields a text field (varchar 1000) and a date field.

Now I would like to filter for a date range in the date field, lets say between '1/1/16' and '2/1/16' but that should only be in case the text field contains a certain wording something like '%My Wording%'.

However in case the text field does not contain that word then I would want to see the record regardless of the date.

What is the simplest way to compose that?
(simple I mean in terms of clearness in reading, but at the same time should not compromise performance)
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
  • 8
  • 3
12 Comments
 
LVL 34

Expert Comment

by:Norie
ID: 41896751
How are you currently building the SQL statement?
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41896758
Hi,

I'm using Access VBA to build that, its an ADP project so needs to be t-sql syntax.

Here is some extract of code...
    If Nz(txtLetterRange, "") <> "" Then
        If InStr(txtLetterRange, "-") > 0 Then
            sStr = sStr & " And  EmpName like '[" & txtLetterRange & "]%'"
        Else
            sStr = sStr & " And  EmpName like '" & txtLetterRange & "%'"
        End If
    End If

Open in new window

Thanks,
Ben
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41896805
Here is the sample code for you --

WHERE DateFields >=  
                  CASE WHEN Wording LIKE '%My Wording%' THEN '1/1/16' ELSE DateFields END
AND
        DateFields <=  
                  CASE WHEN Wording LIKE '%My Wording%' THEN '2/1/16' ELSE DateFields END

CREATE TABLE Wordings
(
	 ID INT
	,Wording VARCHAR(100)
	,DateFields DATETIME
)
GO

INSERT INTO Wordings VALUES
(1,'My Wording','1/1/16'),
(2,'My Wording','2/1/16'),
(3,'Pawan','1/1/16'),
(4,'Ramehs','5/1/16'),
(5,'My Wording','5/1/16')
GO

SELECT * FROM Wordings

WHERE DateFields >=  
			CASE WHEN Wording LIKE '%My Wording%' THEN '1/1/16' ELSE DateFields END
AND 
	  DateFields <=  
			CASE WHEN Wording LIKE '%My Wording%' THEN '2/1/16' ELSE DateFields END

Open in new window


Output 

Open in new window


Note - The code is filtering the records (5,'My Wording','5/1/16') since it date is outside our range.

ID          Wording                                                                                              DateFields
----------- ---------------------------------------------------------------------------------------------------- -----------------------
1           My Wording                                                                                           2016-01-01 00:00:00.000
2           My Wording                                                                                           2016-02-01 00:00:00.000
3           Pawan                                                                                                2016-01-01 00:00:00.000
4           Ramehs                                                                                               2016-05-01 00:00:00.000

(4 row(s) affected)

Open in new window


Hope it helps !!
0
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.

 
LVL 4

Author Comment

by:bfuchs
ID: 41896848
Hi Pawan,

Will test & let you know.

Thanks,
Ben
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41896907
Hi,

The example you posted works for two particular dates, but I need it to work for a date range, something the follows

1- >= '1/1/16'

2- between '1/1/16' and '2/1/16'

tried modify your code but getting an error.

Thanks,
Ben
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41896923
Hi Pawan,

Actually disregard my previous post, I see that do work for date range as well.

However I do have an issue with null values, meaning in your example if field wording is lets say 'pawan' and datefields is null it will not come up.

Let me know how to adjust that.

Thanks,
Ben
0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41896928
Try this.

Option 1--

SELECT * FROM Wordings
WHERE ( DateFields >=  
			CASE WHEN Wording LIKE '%My Wording%' THEN '1/1/16' ELSE DateFields END OR DateFields IS NULL )
AND 
	  ( DateFields <=  
			CASE WHEN Wording LIKE '%My Wording%' THEN '2/1/16' ELSE DateFields END OR DateFields IS NULL )

Open in new window



Option 2


SELECT * FROM Wordings
WHERE 
	  ISNULL( DateFields , '' ) >=  CASE WHEN Wording LIKE '%My Wording%' THEN '1/1/16' ELSE ISNULL(DateFields ,'') END
AND 
	  ISNULL( DateFields, '') <=  CASE WHEN Wording LIKE '%My Wording%' THEN '2/1/16' ELSE ISNULL(DateFields ,'') END

Open in new window

0
 
LVL 4

Author Comment

by:bfuchs
ID: 41896934
OK, your second option seems to work, however will do additional testing tom & let you know.

Thanks,
Ben
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41896938
Sure , I will wait for your +ve response. :)

Bye !!
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 41898159
Hi Pawan,

This works great, Thank you!
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41898177
BTW, is there a way to have the 'between '1/1/16' and '2/1/16' when using it in this case?

the reason for asking is that this would be easier for me to integrate into my app, as I have this syntax already for other cases.

Thanks,
Ben
0
 
LVL 4

Author Comment

by:bfuchs
ID: 41898232
Hi Pawan,

Sorry for letting you wait:)
I have another one for you..

https://www.experts-exchange.com/questions/28984854/How-to-place-a-condition-in-a-filter-criteria-in-t-sql-2.html

Thanks,
Ben
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

734 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