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 > DB2 > specify IF CONDITION WITHIN SELECT

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-26-07, 06:15
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
 
Join Date: Jan 2005
Posts: 75
specify IF CONDITION WITHIN SELECT

Hi ,

how to specify if condition within select statement??
Reply With Quote
  #2 (permalink)  
Old 02-26-07, 06:21
guyprzytula guyprzytula is offline
Registered User
 
Join Date: Jun 2006
Posts: 471
do you mean the where condition
select .. from table.. where ..
Is if not specific for sql in stored proc/functions
__________________
Best Regards, Guy Przytula
DB2 UDB LUW certified V6/7/8
Reply With Quote
  #3 (permalink)  
Old 02-26-07, 06:24
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
 
Join Date: Jan 2005
Posts: 75
I mean a simple if condition. like this ( I know the syntax might not be right)
select if ( emp_sal >1000) then emp_grade =500 from emp something like this
Reply With Quote
  #4 (permalink)  
Old 02-26-07, 06:29
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
select case when emp_sal > 1000 then 500 end from emp

You can also use multiple case conditions and also ELSE case.

Sample:
select
case
when emp_sal > 1000 then 5000
when emp_sal > 2000 then 4000
else 0
end
from emp


You can also use nested case statements.

Sample:
select
case
when emp_sal > 1000 then case when xxx = 3 then x else y end
when emp_sal > 2000 then 4000
else 0
end
from emp


For more samples and any other SQL questions I recommend DB2 Cookbook - the best free SQL book ever seen: http://mysite.verizon.net/Graeme_Birchall/id1.html

Hope this helps,
Grofaty
Reply With Quote
  #5 (permalink)  
Old 02-26-07, 07:02
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
 
Join Date: Jan 2005
Posts: 75
Thanks for the swift response!!!!
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