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 > Database Server Software > Sybase > Problem in executing query in CASE statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-02-10, 02:26
surbhit4u surbhit4u is offline
Registered User
 
Join Date: Jun 2010
Posts: 11
Problem in executing query in CASE statement

Hello All,

I am facing a problem in executing queries with CASE statement.
Based on my condition,(for eg. length), I want to execute different SQL statement.

Problematic sample query is as follows:

Code:
select case when char_length('19480821')=8
                then select count(1) from Patient
            when char_length('19480821')=10
                then select count(1) from Doctor 
    end
Exception:
>[Error] Script lines: 1-5 --------------------------
Incorrect syntax near the keyword 'select'.
Msg: 156, Level: 15, State: 2
Server: sunsrv4z7, Line: 2


I am not able to correct the syntax. I am getting the string for char_length as input from the user.
How can I fire queries based on certain condition?
Is CASE the right choice ? Or do I have to use any other thing.

Please advice me the right way.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 07-06-10, 07:26
reddy_546 reddy_546 is offline
Registered User
 
Join Date: May 2010
Location: Hyderabad, India
Posts: 16
Use following Query

select case when char_length('19480821')=8
then (select count(1) from Patient)
when char_length('19480821')=10
then (select count(1) from Doctor)
end


I think this will work fine.
__________________
-PavanKumar M Reddy
Reply With Quote
  #3 (permalink)  
Old 07-06-10, 07:36
surbhit4u surbhit4u is offline
Registered User
 
Join Date: Jun 2010
Posts: 11
Thanks,
It worked.
Reply With Quote
Reply

Tags
case, query, sql

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