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 not working

Author  Topic 

accessdbguru
Starting Member

26 Posts

Posted - 2012-11-07 : 17:03:58
I have the following CASE statement that needs to look at criteria on multiple columns and it is not working. Please help.


SELECT Invoice, Cost, Amount,
CASE Billed_Type
WHEN (dbo.SJC00999.Cost_Code_Number_1) ('LAK') THEN 'LAKA'
WHEN (dbo.SJC00999.Cost_Code_Number_2) ('LAK') THEN 'LAK1'
WHEN (dbo.SJC00999.Cost_Code_Number_3) ('UN') THEN 'UNBILLED'
WHEN (dbo.SJC00999.Cost_Code_Number_1) ('L3K') THEN 'LA3A'
ELSE 'BILLED'
END

FROM dbo.tbl52908

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-07 : 17:12:17
You need to join the current FROM table that you have (dbo.tbl52908) to the table you are using in the case expression (dbo.SJC00999). How you would join depends on what column(s) can be used to join.

From the code you have written, I could not follow the logic you are trying to implement. Can you describe it?

Also, are you using Microsoft SQL Server? If not, this may not be the right forum.
Go to Top of Page

accessdbguru
Starting Member

26 Posts

Posted - 2012-11-07 : 17:21:32
Yes I am using SQL Server 2005. Sorry i am using only 1 table in the query. Here is the REVISED SQL:

SELECT Cost_Code_Description AS Description,

CASE BilledType =
WHEN (dbo.SSJC00701.Cost_Code_Number_3) ('UN') THEN 'UNBILLED'
WHEN (dbo.SSJC00701.Cost_Code_Number_1) ('PINK') THEN 'PINKR'
ELSE
'BILLED'
END
AS
Billed_Type
FROM dbo.SSJC00701

Please advise the correct SQL Statement????

thank you.

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-07 : 17:36:16
[code]SELECT Cost_Code_Description AS [Description],
CASE
WHEN Cost_Code_Number_3 = 'UN' THEN 'UNBILLED'
WHEN Cost_Code_Number_1 = 'PINK' THEN 'PINKR'
ELSE 'BILLED'
END AS Billed_Type
FROM dbo.SSJC00701[/code]The case expression is like an if clause - i.e., if Cost_Code_Number_3 = 'UN' is true, then it would return 'UNBILLED' regardless of whether or not Cost_Code_Number_1 = 'PINK'
Go to Top of Page

accessdbguru
Starting Member

26 Posts

Posted - 2012-11-07 : 17:52:46
That works perfect! THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-11-07 : 20:02:20
You are very welcome.
Go to Top of Page
   

- Advertisement -