Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Posts
    50

    Unanswered: 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

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Marc,
    Yes. Try something like:

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

    Andy

  3. #3
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650
    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.

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

  5. #5
    Join Date
    Aug 2001
    Location
    UK
    Posts
    4,650
    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.

  6. #6
    Join Date
    May 2003
    Location
    USA
    Posts
    5,737
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •