all groups > sql server new users > october 2007 >
You're in the

sql server new users

group:

Query help finding empty or null columns



Query help finding empty or null columns Jamie
10/11/2007 12:00:00 AM
sql server new users: I need someway of locating all the columns in a table where the entire
column is either NULL or ''.

Does anyone know an easy way to do this against a table with about 30
columns? Or any suggestions???

Column 1 Column 2 Column 3
-------- -------- --------
1 NNN
2 NNYIUI

In the above example it would let me know that Column 2 doesn't have data
and or if it could let me know about column 1 and 3 I can figure out Column
2 is the one I'm looking for.

Re: Query help finding empty or null columns Roy Harvey (SQL Server MVP)
10/11/2007 12:00:00 AM
This counts the non-NULL/blank values. Note that Column1 was assumed
to be some number data type (int, decimal, etc) and so there is no
code to handle blanks. Any count of zero indicates a column with only
NULL or blank.

SELECT COUNT(*) as Total,
COUNT(Column1) as Column1,
COUNT(CASE WHEN Column2 = ' '
THEN NULL
ELSE Column2
END) as Column2,
COUNT(CASE WHEN Column3 = ' '
THEN NULL
ELSE Column3
END) as Column3
FROM SomeTable

Roy Harvey
Beacon Falls, CT

On Thu, 11 Oct 2007 09:51:23 -0400, "Jamie"
[quoted text, click to view]
Re: Query help finding empty or null columns Jamie
10/11/2007 11:54:03 AM
Getting error
Incorrect syntax near the keyword 'FROM'.

END) as Receipt_ASN,

FROM labels

Last two lines of code.

[quoted text, click to view]

Re: Query help finding empty or null columns Jamie
10/11/2007 11:57:55 AM
Found the answer:
[quoted text, click to view]

Re: Query help finding empty or null columns Jamie
10/11/2007 3:10:14 PM
By the way thank you for this it worked great.


[quoted text, click to view]

AddThis Social Bookmark Button