Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
cez
Starting Member
37 Posts |
Posted - 2002-12-19 : 06:08:33
|
| HiI'm creating a temp table on the fly. Some of the columns may be NULL. I need a query that returns all the columns that have all the rows NULL to be able to hide the column in my DataGrid (ASP.NET).Can you help ? Thanks. |
|
|
Onamuji
Aged Yak Warrior
504 Posts |
Posted - 2002-12-19 : 08:05:15
|
you should control that at the application level.. the datagrid is very flexible and allows you to do this sort of thing.... you should not look to do this at the db level ... though I think *maybe* you can do something likeCREATE TABLE #temp (a INT, b VARCHAR(10), c MONEY, d VARCHAR(60))INSERT INTO #temp (a, b, c, d) SELECT itemID, name, unitCost, owner FROM Items WHERE Enabled = 1IF NOT EXISTS(SELECT * FROM #temp WHERE a IS NOT NULL) ALTER TABLE #temp DROP COLUMN aIF NOT EXISTS(SELECT * FROM #temp WHERE b IS NOT NULL) ALTER TABLE #temp DROP COLUMN bIF NOT EXISTS(SELECT * FROM #temp WHERE c IS NOT NULL) ALTER TABLE #temp DROP COLUMN cIF NOT EXISTS(SELECT * FROM #temp WHERE d IS NOT NULL) ALTER TABLE #temp DROP COLUMN dSELECT * FROM #tempDROP TABLE #tempbut don't quote me as saying this will work ... :-pI tried it .. um doesn't quite work so like i said do this at the application level...Edited by - onamuji on 12/19/2002 08:08:07 |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2002-12-19 : 08:31:11
|
| To determine if a column has all NULL values:SELECT CASE WHEN Col1 is Null THEN 1 ELSE 0 END as Col1NullCount, CASE WHEN Col2 is Null THEN 1 ELSE 0 END as Col2NullCount, COUNT(*) as TotalRowsFROMTableAny time ColxNullCount = TotalRows, that column has all Null values.- Jeff |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-12-19 : 22:43:43
|
Small fix of jsmith8858 solution.CREATE TABLE #Test(TestID int,Col1 nvarchar(1),Col2 nvarchar (2))INSERT INTO #Test (TestID, Col1, Col2) VALUES (1,NULL, NULL)INSERT INTO #Test (TestID, Col1, Col2) VALUES (2,1, NULL)INSERT INTO #Test (TestID, Col1, Col2) VALUES (3,NULL, NULL)INSERT INTO #Test (TestID, Col1, Col2) VALUES (4,NULL, NULL)SELECT SUM(CASE WHEN Col1 is Null THEN 1 ELSE 0 END) as Col1NullCount, SUM(CASE WHEN Col2 is Null THEN 1 ELSE 0 END) as Col2NullCount,COUNT(*) as TotalRows FROM #TestDROP TABLE #TestNice looking girl must have walked by and distracted jsmith8858 |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2002-12-20 : 09:24:15
|
Valter -- you know me too well!Yes, SUM()'s might help there..... - Jeff |
 |
|
|
|
|
|
|
|