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

07-27-10, 16:23
|
|
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
|
|

07-27-10, 19:09
|
|
:-)
|
|
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
|
|

07-28-10, 00:55
|
|
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)
|
|

07-28-10, 06:32
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 150
|
|
Quote:
Originally Posted by tonkuma
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
|
|

07-28-10, 07:28
|
|
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.
|
|

07-28-10, 08:55
|
|
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
|
|

07-28-10, 10:20
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
Not HEX() but ASCII() function
Quote:
Originally Posted by tonkuma
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:
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:
Lenny
|
|

07-28-10, 11:43
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
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
|
|

07-28-10, 12:58
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
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.
|

07-30-10, 10:41
|
|
Registered User
|
|
Join Date: Nov 2008
Posts: 41
|
|
|
|

07-30-10, 12:23
|
|
Registered User
|
|
Join Date: Jul 2009
Location: NY
Posts: 886
|
|
Quote:
Originally Posted by phil72
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|