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)
 Select - Case Statement

Author  Topic 

homerjay80
Starting Member

26 Posts

Posted - 2007-03-27 : 13:36:40
Hello,

I'm trying to use the case statement to test a condition ,if meets it overides an existing value, if does not meet the condition it displays the default value.

I'm getting the following error on my Select - Case Statement.
Incorrect syntax near the keyword 'CASE'.

Here is my code:

Select SSN, EmpNo, Company, TestValue, StopDate
CASE when TestValue = 'A' and stopdate is null then Company = 'XXX' else company
end
FROM tables...


Any advise?

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2007-03-27 : 13:49:18
Something like:
SELECT SSN, EmpNo, TestValue, StopDate, 
CASE WHEN TestValue = 'A' AND StopDate IS NULL THEN 'XXX' ELSE Company
END AS Company
FROM tables...


Mark
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-03-27 : 16:24:30
You missed a comma after your StopDate column in the SELECT statement.

************************
Life is short. Enjoy it.
************************
Go to Top of Page

homerjay80
Starting Member

26 Posts

Posted - 2007-03-27 : 20:47:42
Ok,

I'm solid on my case statement, now is it possible to put it after a where clause

SELECT EmpNo,SSN,Code,Company,
case when Code = 'X' and stopdate is Null then 'ERR' else Company End
from ...joined tables...



I keep getting this error when it try it:
Incorrect syntax near the keyword 'End'


Thanks Again
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-03-27 : 22:29:18
Give the column a name as MArk suggested.

************************
Life is short. Enjoy it.
************************
Go to Top of Page
   

- Advertisement -