Search code examples
sql-servert-sqlsql-server-2012

My query is still returning rows with null values


I am trying to write a query that only returns rows where a group of columns does not have any nulls.

I don't want any rows returns if the result has a null in any of these columns:

gameTitle, gameDevTeam, gameType, innerId/outerId, prodCode, teamCode

So I did some searching, and found this question:

Optimize way of Null checking for multiple columns

When I tried to apply that logic to my query, I am still seeing results where one or more of the columns are NULL.

Here is my query:

SELECT  *
FROM  GameData gd
WHERE gd.dev_Status = 002
  AND COALESCE(gd.gameTitle, gd.gameDevTeam, gd.gameType, COALESCE(gd.innerId, gd.outerId), gd.prodCode, gd.teamCode) IS NOT NULL
  AND gd.gameType IN(003, 004)

Is there anything I am doing wrong?


Solution

  • Logically you are asking for the opposite of any column being NULL so the following pattern should hopefully work for you:

    select * 
    from t
    where not( col1 is null or col2 is null or col3 is null or ...);