Previous Thread

8/16/2006 1:14:02 PM    Case When
Greetings everyone.  I need help with a stored procedure.  I need to add a 
 
column to enter a value, either 1,2, or 3 depending on the value of another 
 
field in the query.  For example, if the value of the independent field is 
 
'apple' I need the new dependent field to populate '2'.  I think I need to do 
 
a case when statement, but I can not get it to work.  Is this anywhere close? 
 
case when type_of_fruit= 'apple' then '2' 
 
Do I enter this directly into the column field of the grid pane? Thank you 
 
for any advice or help. 
 
Greg Snidow



8/16/2006 4:47:17 PM    Re: Case When
You are missing the End statement at the end of your Case; here is a 
 
complete example: 
 
Select Case when type_of_fruit = 'apple' then '2' when type_of_fruit = 
 
'orange' then '3' else '0' end as ColorNumber, ... 
 
For simple comparaisons like this, you can also write: 
 
Select Case type_of_fruit when 'apple' then '2' when 'orange' then '3' else 
 
'0' end as ColorNumber, ... 
 
-- 
 
Sylvain Lafontaine, ing. 
 
MVP - Technologies Virtual-PC 
 
E-mail: http://cerbermail.com/?QugbLEWINF 
 
"Greg Snidow" <GregSnidow@discussions.microsoft.com> wrote in message 
 
news:5D355587-B2D6-4B7A-A893-6A3E733D2542@microsoft.com...

8/16/2006 11:46:14 PM    Re: Case When
Hello Greg, 
 
You wrote in conference microsoft.public.access.adp.sqlserver  on Wed, 16 
 
Aug 2006 13:14:02 -0700: 
 
GS> Greetings everyone.  I need help with a stored procedure.  I need to 
 
GS> add a column to enter a value, either 1,2, or 3 depending on the value 
 
GS> of another field in the query.  For example, if the value of the 
 
GS> independent field is 'apple' I need the new dependent field to populate 
 
GS> '2'.  I think I need to do a case when statement, but I can not get it 
 
GS> to work.  Is this anywhere close? 
 
Generally, for the purpose like this the right approach is not hardcoding 
 
the values, but creating a separate table Fruits: 
 
fruit      fruitId 
 
apple   2 
 
orange 3 
 
and getting the value by lookup: select fruitId from fruits where 
 
fruit='apple' 
 
The advantage of this is that later, when you add new fruit, you do it by 
 
adding new row to the Fruits table. Otherwise, you will have to find all 
 
occurences of apples etc. in all places (in the database and in the 
 
applications) where you hardcoded the values - and with the time, there will 
 
be many. 
 
Of course, it's kinda general guidance, for educational purposes. If it's 
 
small one-time piece without the future, CASE is ok. 
 
Vadim Rapp