We help IT Professionals succeed at work.

If in a where clause in t-sql

243 Views
Last Modified: 2017-05-03
I have a procedure that has a condition that a variable @Submitter could be null, could have one value or could have a value list
in my SQL stament where clause i want to have it ignore the filter if it is null how can i do this?

select *
from  [databse].[dbo].[user]
Where
      [databse].[dbo].[user].[DateOfFirstEnrollment] <= @DateOfFirstjoin and
      [database].[dbo].[user].[DateOfDisenrollment] >= @DateOfDisjoin  and
     IF (@Submitter IS NOT NULL)
        BEGIN
           AND [database].[dbo].[user].[Submitter] IN (@Submitter)  
        END
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
A value list as in a delimited text string, or is @Submitter a table variable?
TylerSR. Data Systems Administrator

Author

Commented:
@submitter is a velue or value list. passed through a stored procedure  it could be 'TEXT' or 'ONE,TWO,Three,Text' or null

so the 'IN' would work with the first 2 examples but the null i want to ignore alltogeather in the filter.
CERTIFIED EXPERT
Top Expert 2010

Commented:
And what should happen if @Submitter is null? Should the query return nothing, or should it only consider the date criteria?
TylerSR. Data Systems Administrator

Author

Commented:
if @Submitter has value it would use it as a filer if no value it would not use it as a filter

if @Submitter has no value or is 'all' it would ignor @submitter in the filter
  where  datestart='1/1/2017' and
                    dateend='1'5'2017'

if @Submitter has a single value it would us it varchar value
  where submitter = @submitter and
                    datestart='1/1/2017' and
                    dateend='1'5'2017'

if @Submitter has multi-value it would us it as a list
       where submitter in @submitter and
                    datestart='1/1/2017' and
                    dateend='1'5'2017'
CERTIFIED EXPERT
Top Expert 2010

Commented:
select * 
from  [databse].[dbo].[user] u
Where 
      u.[DateOfFirstEnrollment] <= @DateOfFirstjoin and
      u.[DateOfDisenrollment] >= @DateOfDisjoin  and
      (@Submitter IS NULL OR
      ',' + @Submitter + ',' LIKE '%,' + u.[Submitter] + ',%')

Open in new window

IT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.