I have a question about a Null variable and how to use it in the where clause. (See my example: below)
I want to return the records in this case that have company ABC... and contact is null. But it seems to not like the @Contact when it is null. Any ideas?
Thanks
EB
DECLARE @Customer NVARCHAR(40) = 'ABC Company'
, @Contact NVARCHAR(40) = NULL
SELECT
*
FROM
company
where contact = @Contact AND customer = @Customer
don't use select *
DECLARE @Customer NVARCHAR(40) = 'ABC Company', @Contact NVARCHAR(40) = NULL
SELECT *
FROM dbo.company c
where c.contact = @Contact
AND (@Customer is null or customer = @Customer)
Ok I tried that but it returns even records that Contacts are not null. I only want the records with contact is null.
SELECT Company
FROM dbo.company c
where c.customer = @customer
AND (@contact is null or contact = @Contact)
where c.customer = @customer
AND (contact = @Contact or (@contact is null and contact is null))