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)
 Simple SQL Question (IF or CASE)...

Author  Topic 

Harry C
Posting Yak Master

148 Posts

Posted - 2005-04-20 : 10:43:00
I have a table as follows

NotebookID int 4 (PK)
PersonID int 4
KnowledgeBaseID int 4
DiscussionThreadID int 4
[FreeText] varchar 500
CourseID int 4
NotebookEntryDate datetime 8

In this table, the KnowledgeBaseID, DiscussionThreadID or FreeText can be NULL. So, what I want to do is the in ONE statement (preferably) select what is NOT null.

So, I want say basically
"Get back KnowledgeBaseID If DiscussionThreadID AND FreeText are NULL" OR
"Get back DiscussionThreadID If KnowledgeBaseID AND FreeText are NULL"
OR
"Get back FreeText If KnowledgeBaseID AND DiscussionThreadID are NULL"

WHERE NotebookID = @NotebookID...

I know this is simple, and I am making it complex. That is why I am asking for help writing the statement! Thanks in advance for any guidance!

Harry C

mr_mist
Grunnio

1870 Posts

Posted - 2005-04-20 : 10:47:04
What if more than one condition applies?

Anyway, you can use the CASE statement to achieve what you want to do.

SELECT CASE when blah is null and blah2 is null then blah3
CASE when blah2 is null and blah3 is null then blah
END AS yourvalue from yourtable

-------
Moo. :)
Go to Top of Page

Harry C
Posting Yak Master

148 Posts

Posted - 2005-04-20 : 12:17:37
Your right! This minor tweak solves that problem. Thanks alot

SELECT
(CASE
WHEN KnowledgeBaseID IS NOT NULL then 'Test'
WHEN DiscussionThreadID IS NOT NULL then 'Test'
WHEN [FreeText] IS NOT NULL then 'It worked!'


END) AS NotebookDisplayName FROM Notebook
WHERE NotebookID = @NotebookID
Go to Top of Page
   

- Advertisement -