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 2000 Forums
 Transact-SQL (2000)
 How to Select or not to select a Column conditionally

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-04-29 : 07:38:05
Alex writes "Can a query be created that will check to see whether a column (B) is null (has no data)?
If no data, select columns A,C,D. If data exists in column B, select columns A,B,C,D"

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-04-29 : 07:50:42
what do you mean by that?? i can't really visualize what you mean.
do you want do check all values in column B?

select A, isnull(B, ''), C, D
from MyTable

Go with the flow & have fun! Else fight the flow
Go to Top of Page

KLang23
Posting Yak Master

115 Posts

Posted - 2005-04-29 : 14:55:36
Select
Case When B is NULL Then A End Else B as ColA
Case When B is NULL Then B End Else NULL as ColB
Case When B is NULL Then C End Else NULL as ColC
From TableX


If the columns are of different types (int vs. char) you will have to convert one of them (int) to the type of the other (char).

Case When B is NULL Then Convert(Varchar(5),A) End Else B as ColA

Best of luck,
Kevin
Go to Top of Page

KLang23
Posting Yak Master

115 Posts

Posted - 2005-04-29 : 14:57:44
OOps - syntax check - place the "End" after the "Else" ie:

Select
Case When B is NULL Then A Else B End as ColA
Case When B is NULL Then B Else NULL End as ColB
Case When B is NULL Then C Else NULL End as ColC
From TableX

Cheers,
Kevin
Go to Top of Page
   

- Advertisement -