If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Transact SQL -- how do I use "Case"?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-08-05, 18:03
thele thele is offline
Registered User
 
Join Date: Jun 2003
Location: Ohio
Posts: 108
Question Transact SQL -- how do I use "Case"?

Hello, I am having some transact SQL problems.

ALLOWED:
Code:
	Column1, 
	CASE FOO
		WHEN 1 THEN 'Hello'
		ELSE 'Goodbye'
		END as MyValue,
	Column2
NOT ALLOWED:
Code:
	Column1, 
	CASE FOO
		WHEN > 1 THEN 'Hello'
		ELSE 'Goodbye'
		END as MyValue,
	Column2
The problem seems to be the > symbol. Apparantly the case statement is only for equality checks, and nothing else.

So, what can I do to fix this? I have these kinds of comparisons all over.

~Le
Reply With Quote
  #2 (permalink)  
Old 12-09-05, 01:21
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Did you check the documentation about use of the CASE in Transact SQL? Oracle would allow this:
Code:
SELECT empno, ename,
       CASE
          WHEN sal > 1500
             THEN 'Salary > $1500'
          ELSE 'Salary <= $1500'
       END
  FROM employees;
Can you use it (try, before reading the manual)?
Reply With Quote
  #3 (permalink)  
Old 12-21-05, 08:46
acg_ray acg_ray is offline
Registered User
 
Join Date: Jan 2003
Location: Pittsburgh, PA
Posts: 86
Try this

Column1,
CASE
WHEN FOO > 1 THEN 'Hello'
ELSE 'Goodbye'
END as MyValue,
Column2
Reply With Quote
  #4 (permalink)  
Old 12-22-05, 03:18
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
This is not TransactSQL-specific, but holds for all SQL implementations:
The expression between the words WHEN and THEN must be a full valid expression,
either (as in your first case) in one of the SQL datatypes, or as a full logical expression
(like "FOO > 1" or "FOO IS NOT NULL" or "FOO IN (1,2,3)" etc.)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On