I have the following query :
DECLARE @dt AS DATE = NULL;
SELECT orderid, shippeddate
FROM Sales.Orders
WHERE shippeddate = @dt;
I have some Orders with NULL shippeddate but they are not returned when executing the above query.
I have the following query :
DECLARE @dt AS DATE = NULL;
SELECT orderid, shippeddate
FROM Sales.Orders
WHERE shippeddate = @dt;
I have some Orders with NULL shippeddate but they are not returned when executing the above query.
You need to check for NULL using IS NULL.  So:
WHERE shippeddate = @dt OR (shippeddate IS NULL AND @dt IS NULL)
DECLARE @dt AS DATE = NULL;
SELECT orderid, shippeddate
FROM Sales.Orders
WHERE shippeddate = @dt  or (shippeddate IS NULL AND @dt IS NULL)
If you set ANSI_NULLS to OFF you will get what you want to get but it is not recommended :
SET ANSI_NULLS OFF;
In this case predicate that evaluates to UNKNOWN. Obviously, NOT TRUE is FALSE and NOT FALSE is TRUE. However, NOT UNKNOWN remains UNKNOWN. In another way :
NULL has no value, and so cannot be compared using the scalar value operators.
You have to use ISNULL() .
DECLARE @dt AS DATE = NULL;
SELECT orderid, shippeddate
FROM Sales.Orders
WHERE ISNULL(shippeddate, '99991231') = ISNULL(@dt, '99991231')