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)
 CASE problem

Author  Topic 

brandonl
Yak Posting Veteran

58 Posts

Posted - 2003-02-03 : 14:26:57
I have a table with the following fields:

Name0, Sector1, Sector2

Name0 is a machine name, Sector1 and Sector2 can be a value or a NULL.

What I want to do is: select each distinct name0 and, if Sector1 is a NULL select Sector2 instead. if Sector1 is not NULL then use the value in Sector1.

Here's what I have:

SELECT DISTINCT Name0,
CASE Sector1
WHEN NULL THEN Sector2
ELSE Sector1
END AS Sector
FROM TEMPTABLE

It only seems to be pulling Sector1 anyways. I have removed the DISTINCT to see if that's the issue-no joy. What am I doing wrong?

~BrandonL

Vivaldi
Constraint Violating Yak Guru

298 Posts

Posted - 2003-02-03 : 14:29:34
Well,

I am not a sql genious but
couldn't you just

select name0,Coalesce(Sector1,Sector2) ...

that should do what you want.

_________________________
Beer is healthy, I read it on the internet. It must be true!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-02-03 : 14:38:44
Yes, COALESCE would do very nicely, and taking out your DISTINCT Wasn't causing the problem. The syntax looks good and I ran a test on my end....Are you sure Sector1 has any null balues?

Do a SELECT COUNT(*) FROM yourTable Where Sector1 Is Null

Good Luck

Brett

8-)

Go to Top of Page

brandonl
Yak Posting Veteran

58 Posts

Posted - 2003-02-03 : 14:52:48
COALESCE worked for this application.

But, some have different non null values-so I changed the syntax to this and it worked:

SELECT DISTINCT Name0,
CASE
WHEN (Sector1 IS NOT NULL) THEN Sector1
ELSE Sector2
END AS Sector
FROM TEMPTABLE

~BrandonL
Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2003-02-03 : 16:44:47
This is a little cleaner.

SELECT DISTINCT Name0,
ISNULL(Sector1,Sectore2) As Sector
FROM TEMPTABLE

What do you mean they had different null values???


Go to Top of Page
   

- Advertisement -