9

I'm trying to implement optional parameters in a stored procedure that I have but I am running into a problem. Here's a simplified query to illustrate the issue:

SET ANSI_NULLS OFF

DECLARE @MiddleName VARCHAR(20);
SET @MiddleName = NULL;

SELECT * FROM [Customer]
WHERE [LastName] = 'Torres'
AND [MiddleName] = COALESCE(@MiddleName, [MiddleName])

When I run this query I need to get one row back because one Torres has NULL in the [MiddleName] column. But the query returns zero rows. Using IFNULL() produces the same result. From researching COALESCE, I was under the impression that NULL would be returned if all expressions are NULL. As I am not a SQL expert I assume that I am missing something, but what is it.....

Thanks in advance for any help.

1
  • I'm trying to interpret what you are saying correctly. I think you mean the following: how do you use coalesce to say all rows if null otherwise only rows that match. If that's the case I would do something like COALESCE(@MiddleName, '') = '' OR @MiddleName=[MiddleName] in the where clause. Apr 13, 2011 at 19:50

4 Answers 4

18

The problem is that in sql, "WHERE Null = Null" will never return any rows since Null does not equal itself.

You have to do

SELECT * FROM [Customer]
WHERE [LastName] = 'Torres'
AND ( @MiddleName IS NULL OR [MiddleName] = @MiddleName )
2
  • 6
    Another alternative is: COALESCE([MiddleName],'') = COALESCE(@MiddleName, [MiddleName], '') Mar 11, 2009 at 16:07
  • 2
    using "@var IS NULL" returns a constant which the optimiser can use to shortcut the condition. The suggested from Joel would require '' to be compared against every MiddleName. In David's example, the MiddleName need never be compared as the optimiser knows TRUE OR <anything> is always true.
    – MatBailie
    Mar 11, 2009 at 16:19
4

You state you are looking for the query to return the row where the field MiddleName is NULL. Unfortunately (NULL = NULL) does not return true, it returns NULL.

You need something like...

SELECT * FROM [Customer]
WHERE [LastName] = 'Torres'
AND ([MiddleName] = @MiddleName OR @MiddleName IS NULL)
2

Are you trying to do this?

SELECT * FROM [Customer]
WHERE [LastName] = 'Torres'
AND ([MiddleName] = @MiddleName OR @MiddleName IS NULL)

From what I understand it looks like it.

4
  • you are missing some brackets: AND ([MiddleName] = @MiddleName OR [MiddleName] IS NULL)
    – TrevorD
    Mar 11, 2009 at 15:53
  • hmm, might also need to put the [Middlename] is NULL first
    – TrevorD
    Mar 11, 2009 at 15:54
  • [Middlename] is NULL first? why?
    – gcores
    Mar 11, 2009 at 16:00
  • wrong way around, he's trying to make it that if @MiddleName is NULL it is effectively ignored, making the parameter pseudo optional
    – MatBailie
    Mar 11, 2009 at 16:01
2

Your COALESCE returns NULL when the @MiddleName parameter and the MiddleName column are both NULL, but the test will evaluate to false because a NULL does not equal any other NULL.

To workaround this you should explicitly test the @MiddleName parameter for nullity:

SELECT *
FROM [Customer]
WHERE [LastName] = 'Torres'
    AND (@MiddleName IS NULL OR [MiddleName] = @MiddleName)
1
  • 1
    wrong way around, he's trying to make it that if @MiddleName is NULL it is effectively ignored, making the paramtere pseudo optional
    – MatBailie
    Mar 11, 2009 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.