ORDER BY - DB2 SQL

I have a simple SQL, in DB2...

SELET EVAL_CODE
FROM DB2DBA.TEST_CODE

The values of EVAL_CODE are 'T','C','O','P','Z','D'

if I add "ORDER BY EVAL_CODE DESC" or "ORDER BY EVAL_CODE ASC" to the SQL, the result will be like... 'C','D','O','P','T','Z' or 'Z','T','P','O','D','C'

Is there a way that I can order the result set as 'Z','O','P','C','D','T'?

Thanks,
txman35Asked:
Who is Participating?
 
carsRSTCommented:
Sorry - another idea hit me.  Not in a position to try but think this will work.

Use a Case/When in your order by clause...

SELET EVAL_CODE
FROM DB2DBA.TEST_CODE
order by
case when EVAL_CODE = 'Z' then 1
case when EVAL_CODE = 'O' then 2
...
END

0
 
carsRSTCommented:
You would have to add another field, associate the order you want to the letters, and order by the new field.

EVAL_CODE     EVAL_CODE_ORDER (new field)
   Z                              1
   O                              2
   P                              3
(on down)
0
 
k_murli_krishnaCommented:
Try this:

SELET EVAL_CODE
FROM DB2DBA.TEST_CODE
ORDER BY
CASE EVAL_CODE WHEN 'Z' THEN 1
                             WHEN 'O' THEN 2
                             WHEN 'P' THEN  3
                             WHEN 'C' THEN 4
                             WHEN 'D' THEN 5
                             WHEN 'T' THEN 6
END;

You can try give an alias to the CASE using END EVAL_CODE_ORDER OR END AS EVAL_CODE_ORDER.
0
 
txman35Author Commented:
Thank you very much for all your help.  It works great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.