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
 General SQL Server Forums
 New to SQL Server Programming
 Case Statement problem

Author  Topic 

buffer
Starting Member

6 Posts

Posted - 2007-06-25 : 17:30:29
Hey,

I'm trying to get a different column to display if the first column is null. What's happening though is I get null for the column output when I should be getting the u.first/lastName. I've verified that the u.name exists. Are there any errors in my code?


SELECT
[requestID],
[reqVUNetID],
r.[departmentID],

CASE r.[reqForLastName]
WHEN null THEN
u.lastName + ', ' + SUBSTRING(u.firstName,1,1) + '.'
ELSE
[reqForLastName] + ', ' + SUBSTRING([reqForFirstName],1,1) + '.'
END AS [requestedBy],

CONVERT(CHAR(8),r.[submitDate],10) AS [submitDate],
CONVERT(CHAR(8),r.[dueDate],10) AS [dueDate],
[reqStatus],
d.departmentName

FROM [tblRequest] r
INNER JOIN tblDepartment d ON r.departmentID = d.departmentID
INNER JOIN tblStatus s ON r.reqStatus = s.statusID
INNER JOIN tblUser u ON r.reqVUNetID = u.VUNetID
WHERE (s.statusName = 'unassigned') ORDER BY [dueDate], [submitDate], [reqForLastName]

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-06-25 : 17:37:39
Change your query to:

SELECT
[requestID],
[reqVUNetID],
r.[departmentID],

CASE WHEN r.[reqForLastName] IS NULL THEN
u.lastName + ', ' + SUBSTRING(u.firstName,1,1) + '.'
ELSE
[reqForLastName] + ', ' + SUBSTRING([reqForFirstName],1,1) + '.'
END AS [requestedBy],

CONVERT(CHAR(8),r.[submitDate],10) AS [submitDate],
CONVERT(CHAR(8),r.[dueDate],10) AS [dueDate],
[reqStatus],
d.departmentName

FROM [tblRequest] r
INNER JOIN tblDepartment d ON r.departmentID = d.departmentID
INNER JOIN tblStatus s ON r.reqStatus = s.statusID
INNER JOIN tblUser u ON r.reqVUNetID = u.VUNetID
WHERE (s.statusName = 'unassigned') ORDER BY [dueDate], [submitDate], [reqForLastName]


Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-06-25 : 17:47:08
[code]ISNULL(reqForLastName + ', ' + SUBSTRING(reqForFirstName, 1, 1) + '.', u.lastName + ', ' + SUBSTRING(u.firstName, 1, 1) + '.') AS requestedBy,[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -