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 > Conversion CHAR-field to INT-field in ODER BY

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-23-04, 08:07
c149187 c149187 is offline
Registered User
 
Join Date: Jul 2003
Posts: 50
Conversion CHAR-field to INT-field in ODER BY

Hi

I have a CHAR-field A_NUMBER. Is it possible to convert this field to an INTEGER field to use it by ORDER BY in a SQL-statement.

select name from address where town like 'qwertz' order by A_NUMBER

Marc
Reply With Quote
  #2 (permalink)  
Old 01-23-04, 09:04
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Marc,
Yes. Try something like:

Select name,integer(a_number) as int_num from address where town like 'qwertz' order by int_num;

Andy
Reply With Quote
  #3 (permalink)  
Old 01-23-04, 09:09
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Or


C:\>db2 describe table t

Column Type Type
name schema name Length Scale Nulls

------------------------------ --------- ------------------ -------- ----- -----

I SYSIBM CHARACTER 2 0 Yes


1 record(s) selected.


C:\>db2 select * from t

I
--
1
2

2 record(s) selected.


C:\>db2 "select * from t order by integer(i)"

I
--
1
2

2 record(s) selected.


Cheers
Sathyaram


__________________
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #4 (permalink)  
Old 01-23-04, 10:16
c149187 c149187 is offline
Registered User
 
Join Date: Jul 2003
Posts: 50
Thanks,

Is it possible to order the following by a sql-function:

I have the CHAR's :
1, 3b, 10

I want the order
1 -> 3b -> 10

Marc
Reply With Quote
  #5 (permalink)  
Old 01-23-04, 10:22
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
I think it should be possible ...

Other than int, you can also use DB2 Inbuilt functions in the order by clause ...

I suggest that you try what you want on a test machine ...

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 01-23-04, 10:34
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
You can also ORDER BY simple-integer

simple-integer must be greater than 0 and not greater than the number of columns in the result table. The integer n identifies the nth column of the result table.

For example:

select name, integer(A_NUMBER)
from address
where town like 'qwertz'
order by 2

In any order by clause, the order by column must be in the select list.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
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