IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Replacement to CASE statement in DB2.


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
maheshhh

New User


Joined: 10 May 2005
Posts: 6
Location: Hyderabad

PostPosted: Wed Jul 19, 2006 3:27 pm
Reply with quote

Hi,

I would like to know whether there is any alternate mechanism to CASE statement in DB2. My requirement is as below.

Col1 Col2
----- ------
0001 A
0002 P1
0003 P2
0004 T
0005 E
0006 R
0007 X

My requirement is to select the records sorting them based on Col2 as below.

T
E
X
A
R
P2
P1

Please help me in this regard.

Thanks,
Mahesh.
Back to top
View user's profile Send private message
Jerry

New User


Joined: 16 Sep 2005
Posts: 42

PostPosted: Wed Jul 19, 2006 5:36 pm
Reply with quote

Hi Mahesh,

You said:

sorting them based on Col2 as below

Can you please explain the sort please.
Back to top
View user's profile Send private message
maheshhh

New User


Joined: 10 May 2005
Posts: 6
Location: Hyderabad

PostPosted: Wed Jul 19, 2006 6:47 pm
Reply with quote

Hi Jerry,

Let me put it more clear. I want to have the below order after opening the cursor in the application.

Code:
Col1  Col2
----- ------
0004  T
0005  E
0007  X
0001  A
0006  R
0003  P2
0002  P1


This can be done using the below query.

Code:
SELECT Col1, Col2, CASE Col2
                        WHEN 'T'  THEN '0'
                        WHEN 'E'  THEN '1'
                        WHEN 'X'  THEN '2'
                        WHEN 'A'  THEN '3'
                        WHEN 'R'  THEN '4'
                        WHEN 'P2' THEN '5'
                        WHEN 'P1' THEN '6'
                   END CASE
  FROM Table ORDER BY 3;


Thanks,
Mahesh.
Back to top
View user's profile Send private message
Jerry

New User


Joined: 16 Sep 2005
Posts: 42

PostPosted: Wed Jul 19, 2006 8:41 pm
Reply with quote

Hi Mahesh,

How about using TRANSLATE?

You can try this:

SELECT Col1, TRANSLATE(REPLACE(REPLACE(COL2,'P1','6)),'P2','5'),'01234','TEXA') FROM Table ORDER BY 3;
Back to top
View user's profile Send private message
maheshhh

New User


Joined: 10 May 2005
Posts: 6
Location: Hyderabad

PostPosted: Thu Jul 20, 2006 3:53 pm
Reply with quote

Hi Jerry,

Thanks for the suggestion. I need to check the performance of the replace statement.

Thansks & Regards,
Mahesh.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts Zunit Test case editor error Testing & Performance 4
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts Relate COBOL statements to EGL statement All Other Mainframe Topics 0
No new posts process statement for SUPREC, CMPCOLM... TSO/ISPF 4
Search our Forums:

Back to Top