Unix Technical Forum

Select CASE returning NULL value

This is a discussion on Select CASE returning NULL value within the SQL Server forums, part of the Microsoft SQL Server category; --> I have the following CASE statement in a view: (SELECT CASE WHEN ISNULL([dbo].[tblContact].[JobTitleID], NULL) = NULL THEN [dbo].[f_ContactNameFL]([dbo].[tblContact].[ContactID]) ELSE ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-29-2008, 09:26 PM
sbowman@comcast.net
 
Posts: n/a
Default Select CASE returning NULL value

I have the following CASE statement in a view:

(SELECT CASE
WHEN ISNULL([dbo].[tblContact].[JobTitleID], NULL) = NULL
THEN [dbo].[f_ContactNameFL]([dbo].[tblContact].[ContactID])
ELSE [dbo].[f_ContactNameFL]([dbo].[tblContact].[ContactID]) + ', ' +
[dbo].[tlkpJobTitle].[Title]
END) AS ContactNameAndTitle

It works perfectly when dbo.tblContact.JobTitleID is NOT null. When it
is null it should return a contact name but it is returning null. The
problem is not f_ContactNameFL. There is another column in the same
view that uses that function and it works perfectly. What am I doing
wrong?

Thanks,
Shelley
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-29-2008, 09:26 PM
Plamen Ratchev
 
Posts: n/a
Default Re: Select CASE returning NULL value

You cannot check NULL for equality (it is unknown, so it doesn't equal
another unknown). The correct way to check if expression is NULL is using IS
NULL:

CASE WHEN [dbo].[tblContact].[JobTitleID] IS NULL THEN ...

The code that you posted can be simplified using the COALESCE function:

[dbo].[f_ContactNameFL]([dbo].[tblContact].[ContactID]) +
COALESCE(', ' + [dbo].[tlkpJobTitle].[Title], '') AS ContactNameAndTitle

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:10 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com