View previous topic :: View next topic
|
Author |
Message |
maheshhh
New User
Joined: 10 May 2005 Posts: 6 Location: Hyderabad
|
|
|
|
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 |
|
|
Jerry
New User
Joined: 16 Sep 2005 Posts: 42
|
|
|
|
Hi Mahesh,
You said:
sorting them based on Col2 as below
Can you please explain the sort please. |
|
Back to top |
|
|
maheshhh
New User
Joined: 10 May 2005 Posts: 6 Location: Hyderabad
|
|
|
|
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 |
|
|
Jerry
New User
Joined: 16 Sep 2005 Posts: 42
|
|
|
|
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 |
|
|
maheshhh
New User
Joined: 10 May 2005 Posts: 6 Location: Hyderabad
|
|
|
|
Hi Jerry,
Thanks for the suggestion. I need to check the performance of the replace statement.
Thansks & Regards,
Mahesh. |
|
Back to top |
|
|
|