SQL Server : check if variable is Empty or NULL for WHERE clause

Just use

If @searchType is null means ‘return the whole table’ then use

WHERE p.[Type] = @SearchType OR @SearchType is NULL

If @searchType is an empty string means ‘return the whole table’ then use

WHERE p.[Type] = @SearchType OR @SearchType=""

If @searchType is null or an empty string means ‘return the whole table’ then use

WHERE p.[Type] = @SearchType OR Coalesce(@SearchType,'') = ''

Leave a Comment