Hi experts: I have a table Called tblAlarm and it has some records like this:

I have another table for determine what user see what message:

Now I want to write a query to show Messages that user has not seen if message didinot expired.(for example it’s year between BeginYear and EndYear and so on …). I write this query:
SELECT *
FROM
tblAlarms LEFT OUTER JOIN tblUsersAlarms tua ON tblAlarms.Id=tua.MessageID
WHERE @CurrentYear BETWEEN tblAlarms.BeginYear AND tblAlarms.EndYear
AND @CurrentMonth BETWEEN tblAlarms.BeginMonth AND tblAlarms.EndMonth
AND @CurrentDay BETWEEN tblAlarms.BeginDay AND tblAlarms.EndDay
AND @CurrentHour * 60 + @CurrentMinute BETWEEN tblAlarms.BeginHour*60 + tblAlarms.BeginMinute AND tblAlarms.EndHour*60 + tblAlarms.EndMinute
--AND (tua.UserID <> 128 AND tua.UserID IS NULL)
and it returns :

but if I unComment last line it does not return any record.How I can determine what messages that users has not been seen?
thanks
The result of comparing a NULL with a value is UNKNOWN. A filter condition only selects rows where the conditions evaluate to TRUE, so rows where the condition evaluates to FALSE or UNKNOWN are not selected.
The
IS NULLandIS NOT NULLtests are different and always produce a TRUE or FALSE answer. You also cannot writecolumn = NULLorcolumn != NULL(at least, the SQL standard does not allow that; some SQL dialects may).Your condition is:
If the value of
tua.UserIDis NULL, then the second term evaluates to TRUE, but the first will then evaluate to UNKNOWN, and UNKNOWN and TRUE is UNKNOWN, which is not TRUE, so the row is not selected.If the value of
tua.UserIDis not NULL, then the second term evaluates to FALSE, so the overall condition evaluates to FALSE, and so the row is not selected.Hence, when you add the condition, nothing is selected, as you observed.
More typically, your condition would be either of these two:
The double condition in the first alternative is in fact redundant. If
tua.UserIDis NULL, the first term will evaluate to UNKNOWN, so the overall condition would not be TRUE and the row would not be selected. The second alternative is quite useful.