SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Removing Null Columns


Removing Null Columns

Author
Message
edgar58
edgar58
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 64
Hi, I'm trying to remove null columns. The columns that need to be removed are not always the same; I run several queries for certain data sets and the resulting columns can vary.

For example I have a table that contains:
Account,
Fname,
Lname,
DOB,
Program1,
Program2,
Program3,
Program4,
Program5

However, Program2 and Program5 or any other program may contain all null values. So I am running the following query to create a new table that only contains the columns that contain data.

Declare @ColumnList as Varchar(100)
Set @ColumnList as ''
If Exists
(Select Program1 from Table where Program1 is not null)
Begin
Set @ColumnList = @ColumnList+'Program1,'
End
If Exists
(Select Program2 from Table where Program2 is not null)
Begin
Set @ColumnList = @ColumnList+'Program2,'
End
...And so on, Then:

Begin
Declare @Command as varchar(500)
Set @Command = 'Select Account,Fname,Lname,DOB' + @ColumnList + ' EndColumn Into NewTable from Table'
exec (@Command)

The "EndColumn" is added to account for the last "," in the @ColumnList variable.

The resulting table should contain:
Account,
Fname,
Lname,
DOB,
Program1,
Program3,
Program4,
EndColumn

However, I am getting an error for incorrect syntax next to "Table" when @Command runs. All help or alternate way to remove columns is appreciated. Thanks!
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12678 Visits: 5478
Just output the content of your @Command before executing so you can see the built SQL statement.
You can debug it and find what is wrong with it.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218301 Visits: 41995
edgar58 (11/2/2012)
Hi, I'm trying to remove null columns. The columns that need to be removed are not always the same; I run several queries for certain data sets and the resulting columns can vary.

For example I have a table that contains:
Account,
Fname,
Lname,
DOB,
Program1,
Program2,
Program3,
Program4,
Program5

However, Program2 and Program5 or any other program may contain all null values. So I am running the following query to create a new table that only contains the columns that contain data.

Declare @ColumnList as Varchar(100)
Set @ColumnList as ''
If Exists
(Select Program1 from Table where Program1 is not null)
Begin
Set @ColumnList = @ColumnList+'Program1,'
End
If Exists
(Select Program2 from Table where Program2 is not null)
Begin
Set @ColumnList = @ColumnList+'Program2,'
End
...And so on, Then:

Begin
Declare @Command as varchar(500)
Set @Command = 'Select Account,Fname,Lname,DOB' + @ColumnList + ' EndColumn Into NewTable from Table'
exec (@Command)

The "EndColumn" is added to account for the last "," in the @ColumnList variable.

The resulting table should contain:
Account,
Fname,
Lname,
DOB,
Program1,
Program3,
Program4,
EndColumn

However, I am getting an error for incorrect syntax next to "Table" when @Command runs. All help or alternate way to remove columns is appreciated. Thanks!


For starters, you're missing a comma after the DOB column and @column list has no leading comma.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
edgar58
edgar58
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 64
Thank you all for your input. It's working like a charm!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search