Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Selecting a column based on a Condition

Author  Topic 

abhwhiz
Starting Member

37 Posts

Posted - 2007-09-19 : 22:41:45
I am writing a Select query where I need to select a column based on a condition. Its like this

Select col1,col2 from tab1 where col1=val, col2=val

Now what I want to to is to select the "col2" only if it is not empty.
I want to select it otherwise. I thought CASE would help me, but I am not able to do it. Please help.

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-09-20 : 00:13:05
select the column anyway, and deal with the fact that it is empty at the frontend application.


-ec
Go to Top of Page

abhwhiz
Starting Member

37 Posts

Posted - 2007-09-20 : 00:25:53
do you think it is not possible in SQl Server?
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-09-20 : 00:47:24
quote:
Originally posted by abhwhiz

do you think it is not possible in SQl Server?



it would be possible, but it is silly to do. Each row you brought back in your select might have differing number of columns.

Why don't you explain what you really want and we can help you with thta solution instead.



-ec
Go to Top of Page

associate01
Starting Member

3 Posts

Posted - 2007-09-20 : 12:01:27
@abhwhiz:

When you say you want to select col2 only if it is not empty do you mean only if every row in col2 is not empty? Or do you mean if a single row is not empty? Please give a little more detail about what you're doing and why you want to do it so we can answer the question.
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-20 : 13:27:19
quote:
Originally posted by associate01

@abhwhiz:

When you say you want to select col2 only if it is not empty do you mean only if every row in col2 is not empty? Or do you mean if a single row is not empty? Please give a little more detail about what you're doing and why you want to do it so we can answer the question.



Based on what he wrote:

Select col1,col2 from tab1 where col1=val, col2=val

He is asking that if col2=val returns a null then to not display col2 in the select list.

As eyechart said, it is better to just handle that in the front-end, not sure what the requirement would be to do it in SQL?



Future guru in the making.
Go to Top of Page

abhwhiz
Starting Member

37 Posts

Posted - 2007-09-21 : 02:03:18
Actually there was a stored procedure to which i was passing three parameters in which the second one we might pass as '' at times, in that case I didnt want a particular column to be selected. I used COALESCE to solve the issue. First i put a if condition to store NULL to the variable, if '' is passed and used COALESCE to select the value.

Thanks a lot for you help eyechart, i have already told the dev team to have a look.
Go to Top of Page
   

- Advertisement -