Results 1 to 10 of 10
  1. #1
    Anna987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    9

    SQL (Query) sort the numeric values as string value

    Hello,

    I have a problem with sorting on a numeric value, the sorting looks like a sort of a string (while field schuifid in table is numeric).
    This is the sql :
    SELECT schuifid, omschrijving FROM schuiven UNION SELECT"<Alle>"," " FROM schuiven ORDER BY schuifID;
    The result = 1,10,100,101,11,110... in stead of 1,10,11,100,101,110.



    Can someone help me with this?

    Thanks in advance.

    Anna

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    What is <Alle>? Why is the field in UNION being treated as string? Format the number field so every value has the same number of characters padding with leading zeros.

    ORDER BY Format(schuifid, "0000")

    Use however many 0's needed for the length of largest possible number.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Anna987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    9
    The sorting is on a dropdown list and <Alle> is added as an item with union select to the dropdownlist, this field is not the field where the sorting is done.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Okay, so are you saving schuifid into the bound field? Don't you want this column hidden so users don't see the number, just omschrijving values? Consider:

    SELECT schuifid, omschrijving FROM schuiven
    UNION SELECT Null, "<Alle>" FROM schuiven;

    ColumnCount: 2
    ColumnWidths: 0";1"

    Note ORDER BY not needed.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    To add something by way of an explanation - By adding 'Alle' to the initial Union query you are forcing access to implicitly convert your number data to text.
    'Alle' can only be text , whereas 1,2,3,4 etc could be either. Access /SQL has to treat values in a way it can handle.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    another way round this would be to include the values twice, one is numeric to be sorted on and once for display

    SELECT schuifid, omschrijving AS N, omschrijving AS S FROM schuiven
    UNION SELECT Null, 0, "<Alle>" FROM schuiven;

    adjust column count, widths and bound column accordingly

    sort on column N

  7. #7
    Anna987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    9
    I now understand why this happens. The union select <All> is added to the numeric field and then i ORDER BY this field.
    So the <Alle> (string) is in de number colomn, thats why the sorting is like string and not as numeric.
    The reason why i add the <All> is to see all the records in a query when i select <All> in the dropdown list. I use this <all> value ias criteria in a query (IIf([forms]![leggerindeling].[OpzoekSchuifnummer]="<alle>";[schuifnummer];[forms]![leggerindeling].[OpzoekSchuifnummer])
    How can I still use the <All> and also sort like a numeric field 1,2,3,4,5,100,101,...?

    Thanks in advance.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    see previous post (#6)

  9. #9
    Anna987 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Feb 2015
    Posts
    9
    I managed like Ajax said, make a new colomn with the same numeric value and sort this colomn.

    Thanks to all for your support.

    Anna

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Did you read post 4?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 08-16-2018, 06:56 AM
  2. Replies: 19
    Last Post: 09-25-2015, 10:26 PM
  3. Setting Up DB with numeric values
    By dashingirish in forum Access
    Replies: 4
    Last Post: 02-17-2012, 03:41 PM
  4. Replies: 3
    Last Post: 04-19-2011, 06:41 PM
  5. order by - string and numeric
    By pen in forum Queries
    Replies: 10
    Last Post: 05-20-2009, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums