MySQL - Selecting rows with null columns
isnull, mysql, select, sql
Solution
the best answer that does not need to hard-code the column names is:
DECLARE @sqlStr VARCHAR(max) = (
SELECT stuff((
SELECT 'and ' + c.NAME + ' is null '
FROM sys.columns c
WHERE object_name(object_id) = 'yourtablename'
ORDER BY c.NAME
FOR XML PATH('')
), 1, 3, '')
)
SET @sqlStr = 'select * from ' + yourtablename + ' where ' + @sqlStr
PRINT @sqlStr
EXEC (@sqlStr)
Problem
How can I select any row that contains empty or null column? I'm trying to run a check on my table, in which I want to see if any of my rows contain a column that doesn't hold a value.. example table: demo ``` +----+------+------+ | ID | col1 | col2 | +----+------+------+ | 1 | VAL1 | Val2 | | 2 | NULL | Val2 | | 3 | VAL1 | NULL | +----+------+------+ ``` I want the query to return rows 2-3 , noting that I have many columns in actual table so I don't want to include it in the query with 'where or'. can it be done with mysql?