From: Holger Schröter on
Hi,
i have a table with over 20 columns.
I need a select statement what return only columns with contents

I hope you understand it. My english isn't so good

Thanks

SQL-Server 2000
From: Erland Sommarskog on
Holger Schr�ter (HolgerSchrter(a)discussions.microsoft.com) writes:
> i have a table with over 20 columns.
> I need a select statement what return only columns with contents
>
> I hope you understand it. My english isn't so good

I'm afraid that that is not a request that makes much sense. A SELECT
statement produces a fixed number of columns, and you cannot decide
that some columns are to be returned only if some condition is true.

What you can do is to first run some statement that examines the data, and
then constructs a SELECT statement dynamically. But I can't say that
seems very useful.

I think you should return to the drawingboard and reconsider your
underlying problem.


--
Erland Sommarskog, SQL Server MVP, esquel(a)sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
From: --CELKO-- on
I think you want to find columns that are all NULLs.

SELECT 'MyDatabase',
CASE WHEN MAX(user_id) IS NULL
THEN 1 ELSE 0 END AS 'user_id',
CASE WHEN MAX(last_name) IS NULL
THEN 1 ELSE 0 END AS 'last_name',
CASE WHEN MAX(first_name) IS NULL
THEN 1 ELSE 0 END AS 'first_name'
etc
FROM MyDatabase;

 | 
Pages: 1
Prev: Detect Valid Full backup
Next: SELECT question?