I am trying to implement optional parameters in a stored procedure that I have, but I have a problem. Here's a simplified query to illustrate the problem:
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 return one row, because one Torres has NULL in the [MiddleName] column. But the query returns null rows. Using IFNULL () gives the same result. From the COALESCE study, I got the impression that NULL would be returned if all the expressions were NULL. Since I'm not an SQL expert, I assume that I am missing something, but what is it .....
Thanks in advance for your help.
sql sql-server
mpeterb
source share