View previous topic :: View next topic |
Author |
Message |
spalanis Beginner
Joined: 19 Jul 2006 Posts: 32 Topics: 15
|
Posted: Wed Nov 18, 2009 3:11 am Post subject: Help Needed in "ORDER BY" on a character |
|
|
In DB2 UDB Stored Procedure, I have a Character field of length 10. This field has both numeric and character data. See example below.
CA2004
CA2300
65161
100803
When I sort this data, I have the output in the below order.
100803
65161
CA2004
CA2300
But I need it in the below order.
65161
100803
CA2004
CA2300
Thanks in advance for any of your assistance |
|
Back to top |
|
|
CZerfas Intermediate
Joined: 31 Jan 2003 Posts: 211 Topics: 8
|
Posted: Wed Nov 18, 2009 5:35 am Post subject: |
|
|
Hello Spalanis,
whatever you want to do, you are not performing a character sort on your output column. Perhaps you should consider adding leading zeroes to the numeric values, therefore "aligning them to the right".
If you sort the rows
065161
100803
CA2004
CA2300
you should achive the desired result.
regards
Christian |
|
Back to top |
|
|
spalanis Beginner
Joined: 19 Jul 2006 Posts: 32 Topics: 15
|
Posted: Wed Nov 18, 2009 5:39 am Post subject: |
|
|
I thought of adding a leading space to the form. But How can I achieve it through DB2 UDB SQL. Please advice. |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12370 Topics: 75 Location: San Jose
|
Posted: Wed Nov 18, 2009 12:23 pm Post subject: |
|
|
spalanis,
Try using the following ORDER BY (untested)
Code: |
ORDER BY
SUBSTR('0000000000',1,10 - LOCATE(' ',your_col)) ||
SUBSTR(your_col,1,LOCATE(' ',your_col)-1)
|
|
|
Back to top |
|
|
spalanis Beginner
Joined: 19 Jul 2006 Posts: 32 Topics: 15
|
Posted: Thu Nov 19, 2009 6:49 am Post subject: |
|
|
Kolusu,
As always, thanks a lot for your selfless assistance here.
Srini |
|
Back to top |
|
|
NASCAR9 Intermediate
Joined: 08 Oct 2004 Posts: 274 Topics: 52 Location: California
|
Posted: Fri Nov 20, 2009 1:25 pm Post subject: |
|
|
Ok, I created a test table with a 10 character col. with the values above.
I tried my query with kolusu (UNTESTED) order by and my results where as follows:
Code: |
SELECT CHAR1
FROM FDBEL.RON_TEST
ORDER BY
SUBSTR('0000000000',1,10 - LOCATE(' ',CHAR1)) ||
SUBSTR(CHAR1,1,LOCATE(' ',CHAR1)-1)
[b]CHAR1[/b]
CA2004
CA2300
65161
100803
SELECT CHAR1
FROM FDBEL.RON_TEST
ORDER BY
SUBSTR('0000000000',1,10 - LOCATE(' ',CHAR1)) ||
SUBSTR(CHAR1,1,LOCATE(' ',CHAR1)-1) DESC
[b]CHAR1[/b]
100803
65161
CA2300
CA2004
|
spalanis Did this really work for you? Is the order above the correct order? _________________ Thanks,
NASCAR9 |
|
Back to top |
|
|
Dibakar Advanced
Joined: 02 Dec 2002 Posts: 699 Topics: 63 Location: USA
|
Posted: Tue Nov 24, 2009 1:47 pm Post subject: |
|
|
Append leading spaces instead of zeroes -
Code: | ORDER BY
SUBSTR(' ',1,10 - LOCATE(' ',your_col)) ||
SUBSTR(your_col,1,LOCATE(' ',your_col)-1) |
|
|
Back to top |
|
|
NASCAR9 Intermediate
Joined: 08 Oct 2004 Posts: 274 Topics: 52 Location: California
|
Posted: Tue Nov 24, 2009 2:11 pm Post subject: |
|
|
Dibakar, Thanks for your reply. I did try it and the results are still not like spalanis asked for in his first post. _________________ Thanks,
NASCAR9 |
|
Back to top |
|
|
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Tue Nov 24, 2009 2:18 pm Post subject: |
|
|
To get the output from "high" to "low", try ORDER BY . . . DESC. _________________ All the best,
di |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12370 Topics: 75 Location: San Jose
|
Posted: Tue Nov 24, 2009 2:18 pm Post subject: |
|
|
NASCAR9/Dibakar,
I think OP just wanted a regular sort but with trailing spaces the values with numerics would be sorted differently. I basically gave him a query which would do the padding.
Kolusu |
|
Back to top |
|
|
Dibakar Advanced
Joined: 02 Dec 2002 Posts: 699 Topics: 63 Location: USA
|
Posted: Tue Nov 24, 2009 2:27 pm Post subject: |
|
|
I think spalanis is more interested in 65161 and 100803.
None of the original list he gave appears to be sorted. |
|
Back to top |
|
|
NASCAR9 Intermediate
Joined: 08 Oct 2004 Posts: 274 Topics: 52 Location: California
|
Posted: Tue Nov 24, 2009 2:51 pm Post subject: |
|
|
papadi wrote: | To get the output from "high" to "low", try ORDER BY . . . DESC. |
I did try it both ways. Refer to my post above and you will see that was part of my testing.
Since spalanis has not responded I will take kolusu advice and bow out. _________________ Thanks,
NASCAR9 |
|
Back to top |
|
|
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Tue Nov 24, 2009 3:11 pm Post subject: |
|
|
Quote: | Refer to my post above and you will see that was part of my testing.
| Sorry 'bout that - i looked and managed to overlook the DESC. . .
And then i also "read" that output wrong. . .
And it is only Tuesday. . .
Mercy,
di |
|
Back to top |
|
|
|
|