Solved

remove null column in SQL

Posted on 2011-02-11
11
438 Views
Last Modified: 2012-06-21
Is it possible removed any columns that is null?
sample select *from table1
0
Comment
Question by:VBdotnet2005
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 3

Expert Comment

by:sunezapa
Comment Utility
remove rows by Select * from table1 where column is not null
and
show nulls as something else by Select ISNULL(column,'') from...

but what do you exactly means with "Remove Columns"?
0
 

Author Comment

by:VBdotnet2005
Comment Utility
let's say it a result has any column that is null, I want to be able to remove it from the result
0
 

Author Comment

by:VBdotnet2005
Comment Utility
select field1, field2, field3 from tabl1

result

john      doe      null
jim      doe      null
james      doe      null
            

I dont want to get null column

Just this

john      doe       
jim      doe       
james      doe       

0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
SELECT *
FROM YourTableNameGoesHere
WHERE YourColumnNameGoesHere IS NOT NULL
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Ah, that is different.  You will have to handle that in your front-end.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Expert Comment

by:sunezapa
Comment Utility
try the ISNULL thing...
Select  field1, field2, ISNULL(field3,'') from table1

Open in new window

it will not return any NULL values (in example an empty string is returned instead)...
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
If your result is

john      doe      null
jim      doe      null
james      doe      abc

What would be your expected result?
           
0
 

Author Comment

by:VBdotnet2005
Comment Utility
Above three columns
If the entire column are null,I want to remove it
If partials, leave it alone
0
 
LVL 3

Expert Comment

by:sunezapa
Comment Utility
no SQL code can know what "the next records" will be, so it is not possible to decide to bring a column or not at runtime.
Do the "filtering" in your frontend.
Maybe ask more detailed what the purpose is - there may be easy solutions, other than what you describe...
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 125 total points
Comment Utility
If you are unable to solve this in the front-end (though why you would even consider doing this is beyond me) and are adamant to do it with T-SQL alone then consider something like this:
IF EXISTS (
	SELECT 1
	FROM dbo.YourTable
	WHERE YourColumn IS NOT NULL)
	SELECT Col1, Col2, YourColumn 
	FROM dbo.YourTable
ELSE
	SELECT Col1, Col2 
	FROM dbo.YourTable

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
What is your SQL Server version?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now