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 > Using a column as a result in CASE statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-03, 13: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, 14: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, 14: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, 07:56
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
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://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 06-24-03, 10: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

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