Original Question:
I am new to SQL server and can't quite find what I am looking for. I'm trying to create this logic in SQL:
IF [column] IS NULL THEN
( SELECT [value] as [columnname] )
ELSE
( SELECT [column] WHERE [column_value] IS NOT NULL )
Basically, if an entire column is null, I want to select and fill the whole column with a certain value.
Else, if that column is not completely null, I only want to select its non-null values (coalesce?).
What is the best way to do this using SQL Server 2005?
Answer (thanks to Josh and everyone else!)
IF EXISTS(SELECT myColumn
FROM mytable
WHERE myColumn IS NOT NULL)
SELECT myColumn as colName
FROM myTable
WHERE myColumn is NOT NULL
ELSE
SELECT [nullReplacementValue] as colName
FROM myTable
Earlier clarification for the confused:
EDIT: For example, if I were given an input table as such:
ColA ColB ColC
--------------------
A null null
B null 1
C null null
If I were running this SELECT on ColB, it should return:
ColB
----
null
null
null
(eventually I'd like to replace "null" with some value but that can come later).
If I were running this SELECT on ColC, it should return:
ColC
----
1