If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Order by Question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-27-10, 16:23
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Order by Question

select S from table(values('0007'), ('@K'), ('DZ')) as s(s) order by s

Result
-----
@K
0007
DZ

Is there a way to sort in ascii order like Javascript, coldfusin, VFP
dsesired Result
----
0007
@K
DZ
Reply With Quote
  #2 (permalink)  
Old 07-27-10, 19:09
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Works for me:
Code:
D:\>db2 "select S from table(values('0007'), ('@K'), ('DZ')) as s(s) order by s"

S
----
0007
@K
DZ

  3 record(s) selected.
Collating sequence is set when you create the database, and you cannot change it afterwards. You can verify what collating sequence your database is configured with by running
Code:
db2 get db cfg | grep -i collating
Reply With Quote
  #3 (permalink)  
Old 07-28-10, 00:55
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Please try:

select S from table(values('0007'), ('@K'), ('DZ'), ('ab')) as s(s) order by HEX(s)
Reply With Quote
  #4 (permalink)  
Old 07-28-10, 06:32
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Unhappy

Quote:
Originally Posted by tonkuma View Post
Please try:

select S from table(values('0007'), ('@K'), ('DZ'), ('ab')) as s(s) order by HEX(s)
Hex-code depends on code-set and how I understood result will be the same:
"Order by S" and "order by HEX(s)".

Kara
Reply With Quote
  #5 (permalink)  
Old 07-28-10, 07:28
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Kara wrote:
Quote:
Hex-code depends on code-set and how I understood result will be the same:
I see.

The resuirement is...
Quote:
Is there a way to sort in ascii order like Javascript, coldfusin
I thought ASCII function, like.
ORDER BY ASCII(s)

But, it returns only the ascii code value of the leftmost character.
So, I have no idea now.
Reply With Quote
  #6 (permalink)  
Old 07-28-10, 08:55
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
select S from table(values('0007'), ('@K'), ('DZ'), ('ab')) as s(s) order by HEX(s)
seems to work fine as Hex code is ordered like ASCII code of these characters
chr Ascii Hex
+ 43 2B
0 48 30
1 49 31
9 57 39
@ 64 40

A 65 41
B 66 42
Z 90 5A
Thank you All
Reply With Quote
  #7 (permalink)  
Old 07-28-10, 10:20
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation Not HEX() but ASCII() function

Quote:
Originally Posted by tonkuma View Post
Kara wrote:

I see.

The resuirement is...

I thought ASCII function, like.
ORDER BY ASCII(s)

But, it returns only the ascii code value of the leftmost character.
So, I have no idea now.
It was a right solution, tonkuma !

Compare:

Code:
select g01, g02
from
(
select 'aa' g01, 'bb' g02 from sysibm.sysdummy1 
union all
select '11' g01, 'dd' g02 from sysibm.sysdummy1 ) aa
order by ascii(g01)
Result is:

Quote:
G01 G02
11 dd
aa bb
And query:

Code:
select g01, g02
from
(
select 'aa' g01, 'bb' g02 from sysibm.sysdummy1 
union all
select '11' g01, 'dd' g02 from sysibm.sysdummy1 ) aa
order by g01
Where result:

Quote:
G01 G02
aa bb
11 dd
Lenny
Reply With Quote
  #8 (permalink)  
Old 07-28-10, 11:43
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down One problem

But, at least one problem:
ASCII function takes first symbol of the string, only:

Code:
select ascii('abcd'), ascii('a')
from sysibm.sysdummy1
Lenny
Reply With Quote
  #9 (permalink)  
Old 07-28-10, 12:58
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Lightbulb Something like this

Maybe something like this:

Code:
with strings (str) as
(
select 'abef'  from sysibm.sysdummy1 
union all
select 'abcd' from sysibm.sysdummy1
union all
select 'afghee' from sysibm.sysdummy1
) 
,
ascii_strs (str, len, k, ascii_str) as
(
select str, length(str), 0, varchar('', 255)  
from strings
union all
select str, len, k + 1, 
ascii_str || Right(digits( ascii(substr(str, k + 1, 1) )), 3)
from ascii_strs  
where k + 1 <= len
) 
select str 
from ascii_strs a1
where k = (select max(a2.k) from ascii_strs a2 where a1.str = a2.str) 
order by a1.ascii_str
Lenny

Last edited by Lenny77; 07-28-10 at 13:02.
Reply With Quote
  #10 (permalink)  
Old 07-30-10, 10:41
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
thanks Lenny
Reply With Quote
  #11 (permalink)  
Old 07-30-10, 12:23
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Talking

Quote:
Originally Posted by phil72 View Post
thanks Lenny
No problem.
You can create UDF function, based on the query which I gave you, and make the ORDER BY using your function.

Lenny
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On