andrefadila andrefadila - 2 years ago 122
SQL Question

Query filter with CASE select NULL field [SQL Server]

How do I simplify this into one WHERE clause with CASE WHEN statement:

@param varchar(1) NULL

if @param = 'Y'
... WHERE field = @param
else if @param = 'N'
... WHERE field = @param OR field IS NULL

Thanks in advance

Answer Source

Like this : without CASE Expression

WHERE (@param='Y' AND field = @param ) OR  (@param='N' AND (field = @param OR field IS NULL))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download