Results 1 to 5 of 5

Thread: SQL Order

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Pilipinas
    Posts
    441

    SQL Order

    Hi Im using this sql code to arrange the item ascendingly;

    "Select * Table1 Order By ItemCode"

    but it display like this..

    1
    10
    2
    3
    4
    5
    6
    8
    9

    what code should i write to make it like this?

    1
    2
    3
    4
    5
    6
    8
    9
    10

  2. #2
    Member
    Join Date
    Jul 2004
    Location
    Pakistan
    Posts
    35
    If you are using ORACLE you need to write the query in the following way:

    "Select * Table1 Order By to_char(ItemCode,'9999')"

    if your are using ACCESS:

    "Select * Table1 Order By format(ItemCode,'0000')"

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    If using SQL Server then...
    Code:
    "SELECT * FROM Table1 ORDER BY CAST(ItemCode AS numeric) ASC"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    And if you cannot guarantee that this field (obviously defined as character) always has numbers in it - use:

    Code:
    SELECT * FROM TABLE1 ORDER BY RIGHT('00'+ITEMCODE,2) ASC

  5. #5
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Or you could change the ItemCode field to a numeric field.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

Posting Permissions

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



Click Here to Expand Forum to Full Width