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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| ||||
| 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 |