4

Here is the create statement for a stored procedure:

Create Procedure SearchCreatedAssignments
(@LessonName Varchar(50), @DateFrom date, @DateTo Date, @LocationCode Varchar(10))
As 
BEGIN 

Basically, I want to write a query that searches the database based on the values of the parameters. For example:

Select *
from dbo.test

where (LessonName = @LessonName)
AND (StartDate = @DateFrom)
AND (EndDate = @DateTo) 
AND (LocationCode = @LocationCode)

Fairly simple, right? However, if any of these parameters are null (or contain an empty string), I would like to omit them from the search, and search by only the parameters that are not null. I was thinking something like this:

--if @LocationCode is null OR @LocationCode = '' -> omit @LocationCode from the search

This is obviously pseudo code. How can I do this? Forgive me if this is a simple task; I am new to SQL.

4 Answers 4

4

Consider the following. If a parameter is NULL or empty, the default value will be the field in question

Select *
from dbo.test
where LessonName   = IsNull(NullIf(@LessonName,''),LessonName) 
 AND  StartDate    = IsNull(NullIf(@DateFrom,''),StartDate) 
 AND  EndDate      = IsNull(NullIf(@DateTo,''),EndDate)
 AND  LocationCode = IsNull(NullIf(@LocationCode,''),LocationCode)
Sign up to request clarification or add additional context in comments.

5 Comments

So, if the parameter is null (or contains an empty string), is it essentially omitted from the search?
@ic3man7019 no, it will just compare the field with itself which will evaluate to true. Most answers do the same.
@ic3man7019 I want to jump in and just point out that it will give the same result as if it were omitted. I use this practice a lot and haven't seen a better one for this kind of queries. And also, many other answers are proposing dynamic sql, I would avoid it whenever it's possible. Consider the effects on performance and security when using dynamic sql.
@ahoxha Thank you. I agree about the dynamic SQL stuff. I do appreciate the answers, though. I'm sure it has its uses.
This is a very common approach to this problem. It can suffer from performance thought because the execution plan will not always be consistent. Take a look at Gail Shaw's article on this type of query for another option using dynamic sql that is safe. sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries
0

You can either write a dynamic SQL statement and execute it using sp_ExecuteSQL in your procedure, or you can get tricky with the SQL like:

Select *
from dbo.test    
where (LessonName = @LessonName)
AND (StartDate = @DateFrom)
AND (EndDate = @DateTo) 
AND (LocationCode = @LocationCode or @LocationCode IS NULL or @LocationCode = '')

Comments

0

You can use the COALESCE function to do so in this way:

where LessonName = coalesce(@LessonName, LessonName)
  AND StartDate = coalesce(@DateFrom, StartDate)
  AND EndDate = coalesce(@DateTo, EndDate) 
  AND LocationCode = coaleasce(@LocationCode, LocationCode)

Although I'm not sure about the empty strings. It will work for null values, in other databases coalesce also handle the empty strings. If it do not work you can use case in the same manner:

LessonName = case when @LessonName is not null and @LessonName != ''
                  then @LessonName else LessonName end

And just use the same logic for the other parameters.

Comments

0

INHO on this case a good way is using a dynamic query.

DECLARE @cmd VARCHAR(MAX);

SET @CMD = 'SELECT * FROM dbo.Text WHERE @Param1 = 'x''; --at least on parameter
IF @PARAM2 IS NOT NULL
BEGIN
    SET @CMD = @CMD + ' AND Param2 = @Param2'
END 
IF @PARAM3 IS NOT NULL
BEGIN
    SET @CMD = @CMD + ' AND Param3 = @Param3'
END 

EXECUTE (@CMD);

2 Comments

Not the best way for sure.
Actually dynamic sql for this type of thing is pretty good from a performance perspective. But you need to parameterize your dynamic sql or it is vulnerable to sql injection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.