Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

remove null column in SQL

Is it possible removed any columns that is null?
sample select *from table1
0
VBdotnet2005
Asked:
VBdotnet2005
  • 3
  • 3
  • 3
  • +1
1 Solution
 
sunezapaCommented:
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
 
VBdotnet2005Author Commented:
let's say it a result has any column that is null, I want to be able to remove it from the result
0
 
VBdotnet2005Author Commented:
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Anthony PerkinsCommented:
SELECT *
FROM YourTableNameGoesHere
WHERE YourColumnNameGoesHere IS NOT NULL
0
 
Anthony PerkinsCommented:
Ah, that is different.  You will have to handle that in your front-end.
0
 
sunezapaCommented:
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
 
SharathData EngineerCommented:
If your result is

john      doe      null
jim      doe      null
james      doe      abc

What would be your expected result?
           
0
 
VBdotnet2005Author Commented:
Above three columns
If the entire column are null,I want to remove it
If partials, leave it alone
0
 
sunezapaCommented:
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
 
Anthony PerkinsCommented:
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
 
SharathData EngineerCommented:
What is your SQL Server version?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now