Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Using a column as a result in CASE statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-03, 14:53
odinsdream odinsdream is offline
Registered User
 
Join Date: Jun 2003
Posts: 10
Using a column as a result in CASE statement

I've got this code:
Code:
CASE WHEN sd.ActiveSite = 1 THEN CASE WHEN sd.Forced IS NULL THEN 'x' ELSE 'F' END ELSE '' END
However, how can I change it so that instead of "F" being returned, I return the value of sd.Forced?
Reply With Quote
  #2 (permalink)  
Old 06-18-03, 15:18
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
Re: Using a column as a result in CASE statement

You have the answer in front of you dude :-) Just do what you want and it works like a charm !!

Code:
CASE WHEN sd.ActiveSite = 1 THEN CASE WHEN sd.Forced IS NULL THEN 'x' ELSE sd.Forced END ELSE '' END

Quote:
Originally posted by odinsdream
I've got this code:
Code:
CASE WHEN sd.ActiveSite = 1 THEN CASE WHEN sd.Forced IS NULL THEN 'x' ELSE 'F' END ELSE '' END
However, how can I change it so that instead of "F" being returned, I return the value of sd.Forced?
Reply With Quote
  #3 (permalink)  
Old 06-18-03, 15:30
odinsdream odinsdream is offline
Registered User
 
Join Date: Jun 2003
Posts: 10
I get an error with that method:

Syntax error converting varchar value 'x' to a column of data type tinyint.

Perhaps the problem is elsewhere? This is the full code: (you've seen it before ;-))
Code:
SELECT MAX(CASE WHEN sd.ActiveSite = 1 THEN case WHEN sd.Forced IS NULL THEN 'x' ELSE sd.Forced END ELSE '' END), ... ...

Did I miss something? It seemed to me that this should have worked, too.
Reply With Quote
  #4 (permalink)  
Old 06-19-03, 08:56
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Quote:
Originally posted by odinsdream
I get an error with that method:

Syntax error converting varchar value 'x' to a column of data type tinyint.

Perhaps the problem is elsewhere? This is the full code: (you've seen it before ;-))
Code:
SELECT MAX(CASE WHEN sd.ActiveSite = 1 THEN case WHEN sd.Forced IS NULL THEN 'x' ELSE sd.Forced END ELSE '' END), ... ...

Did I miss something? It seemed to me that this should have worked, too.

It appears that the parser has decided that the inner case statement should return a tinyint value - presumably because sd.Forced is a tinyint column? So the solution would be to convert sd.Forced to a character string - something like this:

case WHEN sd.Forced IS NULL THEN 'x' ELSE TO_CHAR(sd.Forced) END

I'm not sure if TO_CHAR is the right function name for your DBMS, but if not there must be an equivalent.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #5 (permalink)  
Old 06-24-03, 11:25
odinsdream odinsdream is offline
Registered User
 
Join Date: Jun 2003
Posts: 10
Thanks andrewst! That did solve the problem. Here's the correct code to replace the 'F':

CAST(sd.Forced AS varchar(3))
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

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