Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a table called "test" which has columns like app1,app2,app3,app4,app5.... appn.
I want to select only those columns which value not null.
like if i do as below.

select * from test where id = 10

result will come like.
app1 app2 app3 app4 app5 ......................appn
3000 2000 5000 null null.......................null

i want to select as below.
app1 app2 app3
3000 2000 5000

NOTE:- Table test has 60+ columns and many rows.

so how will be query.?
select * from test where id = 10 and __________
Posted
Updated 23-Jun-15 21:57pm
v2
Comments
CPallini 24-Jun-15 4:02am    
I would re-design the database.
Suvendu Shekhar Giri 24-Jun-15 4:09am    
Check this link. May be it can give you the idea.
Find all those columns which have only null values, in a MySQL table

As far as I know you cannot do that with SQL (and as stated in the comment, I would re-think a database design that is apparently flawed).
You could, however, use a programming language for discarding retrieved null values.
 
Share this answer
 
You 'can' do this, but it's a VERY inefficient query. I would suggest looking at a db redesign or managing the return set for null columns.

Basically, you have to use pivot to turn columns into rows, select IS NOT NULL and then unpivot them back again.

Like I said, this is not at all efficient. The feature is only in T SQL by request, not design.

Not only is it not ideal for that reason, it's also a bugger to get your head around. Here is a link to help you get started:
https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx[^]

I strongly suggest you don't use this technique and find a different solution to your problem as has been suggested in other solutions. By following this route you agree to not find me responsible for any migraines or brain aneurysm that may result

Good luck ^_^
 
Share this answer
 
SQL
declare @query varchar(max)
declare @querybit varchar(max)

select @querybit = (SELECT TOP 1(select SUBSTRING((select '+ ' + name from sys.all_columns
where object_id = OBJECT_ID('yourtablename')
and (system_type_id = 167)
for XML PATH('')),2,20000)) from yourtablename)

select @query = 'SELECT' + @querybit + ' FROM yourtablename'

exec (@query)



could do something a bit like this... you'll need to check sys.types for the correct system_type_id 's and thats hoping that your columns are all the same datatype...

and oops... read the question wrong I was looking at ways of filtering out rows that had null in some columns...
 
Share this answer
 
v2
This can be done through dynamic SQL.
First we can write a query to find out the columns which are not null and then using dynamic sql fetch out the result.

Please see the sample code .



create table test1
(
s_no int not null,
name varchar(10) not null,
address varchar(10) null,
emailid varchar(100) null
)


insert into test1 (s_no, name)
values (1,'A'),(2,'B'),(3,'C')


declare @column_list varchar(8000),
@counter int

set @column_list = ''
set @counter = 0

while (Select max(colid) from syscolumns where id = object_id('test1') and isnullable= 0) > @counter
begin


select @counter = min(colid) from syscolumns where id = object_id('test1') and isnullable= 0 and colid > @counter
select @column_list = @column_list + ',' + (Select name from syscolumns where id = object_id('test1') and isnullable= 0 and colid = @counter)

end


select @column_list = SUBSTRING(@column_list,2,len(@column_list))

declare @sql varchar(8000)
select @sql = 'select ' + @column_list + ' from test1'
print @sql

exec (@sql)

Hope this help.
 
Share this answer
 
Comments
Merajuddin Ansari 27-Jun-15 2:55am    
Thanks a lot all of you for guiding me.
suppose you could concatenate the columns, a null value in any of them will make the computed column NULL
 
Share this answer
 
select * from test where id='10' and isnull(app1,'')<>'' or isnull(app2,'')<>'' or isnull(app3,'');''
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900