MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help Needed in "ORDER BY" on a character

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
spalanis
Beginner


Joined: 19 Jul 2006
Posts: 32
Topics: 15

PostPosted: Wed Nov 18, 2009 3:11 am    Post subject: Help Needed in "ORDER BY" on a character Reply with quote

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
View user's profile Send private message
CZerfas
Intermediate


Joined: 31 Jan 2003
Posts: 211
Topics: 8

PostPosted: Wed Nov 18, 2009 5:35 am    Post subject: Reply with quote

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
View user's profile Send private message
spalanis
Beginner


Joined: 19 Jul 2006
Posts: 32
Topics: 15

PostPosted: Wed Nov 18, 2009 5:39 am    Post subject: Reply with quote

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
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12370
Topics: 75
Location: San Jose

PostPosted: Wed Nov 18, 2009 12:23 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
spalanis
Beginner


Joined: 19 Jul 2006
Posts: 32
Topics: 15

PostPosted: Thu Nov 19, 2009 6:49 am    Post subject: Reply with quote

Kolusu,

As always, thanks a lot for your selfless assistance here.

Srini
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Nov 20, 2009 1:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 699
Topics: 63
Location: USA

PostPosted: Tue Nov 24, 2009 1:47 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Nov 24, 2009 2:11 pm    Post subject: Reply with quote

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
View user's profile Send private message
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Tue Nov 24, 2009 2:18 pm    Post subject: Reply with quote

To get the output from "high" to "low", try ORDER BY . . . DESC.
_________________
All the best,

di
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12370
Topics: 75
Location: San Jose

PostPosted: Tue Nov 24, 2009 2:18 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 699
Topics: 63
Location: USA

PostPosted: Tue Nov 24, 2009 2:27 pm    Post subject: Reply with quote

I think spalanis is more interested in 65161 and 100803.

None of the original list he gave appears to be sorted.
Back to top
View user's profile Send private message Send e-mail
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Nov 24, 2009 2:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Tue Nov 24, 2009 3:11 pm    Post subject: Reply with quote

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. . . Confused

And then i also "read" that output wrong. . . Embarassed

And it is only Tuesday. . .

Mercy,

di
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group