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 2005 Forums
 Transact-SQL (2005)
 Select Case

Author  Topic 

kmdavisjr
Starting Member

2 Posts

Posted - 2007-11-13 : 14:40:37
I am trying to use a simple case statement:

SELECT BudgetAccountID, Initial_Budget_Amount, case Initial_Budget_Amount when > '0' then 'yes' else 'no' end as test
FROM BudgetSummary

I keep getting an Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '>'.
error.

I've tried removing the ticks from the zero, and casting the Initial_Budget_Amount value as int but I still keep getting the error. I've tried to use other boolean operators as well (=, >=, NOT, etc.) and get the same error. What am I missing?

Thanks

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-11-13 : 14:42:52
the syntax is
CASE WHEN <something> THEN <Something> ELSE <Somethingelse> END

case when Initial_Budget_Amount > '0' then 'yes' else 'no' end as test



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

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2007-11-13 : 14:54:19
From handy dandy BOL:
Simple CASE function:

CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END

Searched CASE function:

CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END



[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

kmdavisjr
Starting Member

2 Posts

Posted - 2007-11-13 : 15:05:15
Boy, I feel stupid! Thanks Dinakar!

quote:
Originally posted by dinakar

the syntax is
CASE WHEN <something> THEN <Something> ELSE <Somethingelse> END

case when Initial_Budget_Amount > '0' then 'yes' else 'no' end as test



Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/

Go to Top of Page
   

- Advertisement -