No, you need to apply IFNULL to each column individually.
However, if you had a slightly different requirement, that is, to show the first non-null column from a number of columns, and show a default if they are all null, you could use the COALESCE function like so:
select coalesce(col1,col2,col3,0)
from tbl
The problem with this is that it will return a single column in the result, and not multiple columns corresponding to col1, col2 and col3. Therefore, as long as you want to have multiple columns in your result set, you need to do the null handling on a per-column basis.
No, you need to apply IFNULL to each column individually.
However, if you had a slightly different requirement, that is, to show the first non-null column from a number of columns, and show a default if they are all null, you could use the COALESCE function like so:
select coalesce(col1,col2,col3,0)
from tbl
The problem with this is that it will return a single column in the result, and not multiple columns corresponding to col1, col2 and col3. Therefore, as long as you want to have multiple columns in your result set, you need to do the null handling on a per-column basis.
No, there's no way to use IFNULL on multiple columns at once, you have to do it on each column separately:
SELECT a.id, a.somecol, IFNULL(b.col1, 0) col1, IFNULL(b.col2, "") col2, ...
FROM Table1 AS a
LEFT JOIN Table2 AS b ON <some condition>
Basically, if you have multiple columns that have 'NULL' as a string literal in multiple different rows in the same table, you better so something like this:
UPDATE TableName
SET Col1 = NULLIF(Col1, 'NULL'),
Col2 = NULLIF(Col2, 'NULL'),
Col3 = NULLIF(Col3, 'NULL')
WHERE 'NULL' IN(Col1, Col2, Col3)
The NULLIF function will return NULL if both of it's arguments are the same, or the first argument otherwise.
If you want to do that dynamically for multiple tables, you can do something like this:
DECLARE @SQL nvarchar(max) = '';
SELECT @SQL += 'UPDATE '+ TABLE_NAME + -- Update clause
STUFF( -- Set clause start
(
SELECT ',' + COLUMN_NAME + '= NULLIF('+ COLUMN_NAME +', ''NULL'')'
FROM Information_schema.Columns C
WHERE C.TABLE_NAME = T.TABLE_NAME
FOR XML PATH('')
), 1, 1, ' SET ') + -- Set clause end
' WHERE ''NULL'' IN(' + -- Where clause start
STUFF(
(
SELECT ','+ COLUMN_NAME
FROM Information_schema.Columns C
WHERE C.TABLE_NAME = T.TABLE_NAME
FOR XML PATH('')
),1, 1, '')+ ');' -- Where clause end
FROM Information_schema.Tables T;
EXEC(@SQL);
You can see a live demo on rextester.
SELECT NULLIF (ColA, 'NULL') AS ColA
Thanks!
Meant Null* not Bull. Apologies.