Solved

SQL Syntax

Posted on 2012-03-28
5
332 Views
Last Modified: 2012-06-21
I am trying to order my recordset by the last 1 or 2 numbers from a value that always starts with a letter.  For example the value is B10, I want to order it by 10.

So what is the syntax for this in ORDER BY?

Here is the sql.  The ORDER BY row has psuedo code.

 strSql = "SELECT ServCat, Number, Division, Coded, Reason, ServCatDesc " _
    & "FROM tblServCats " _
    & "WHERE Div = '" & vDiv & "' " _
    & "ORDER BY  mid(ServCat , 1, 2)"

Thanks,
Scott
0
Comment
Question by:ScootterP
  • 2
  • 2
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 37777972
Pad it with zeros to make sure it orders numerically - and I think you might need a corresponding SELECT column for the Order BY:

strSql = "SELECT ServCat, Format(mid(ServCat , 1, 2),'000000') AS SortingColumn, Number, Division, Coded, Reason, ServCatDesc " _
    & "FROM tblServCats " _
    & "WHERE Div = '" & vDiv & "' " _
    & "ORDER BY  Format(mid(ServCat , 1, 2),'000000')"
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 37777979
Try this

& "ORDER BY  CLng(mid([ServCat] , 2))"
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37777994
Sorry - try this:

strSql = "SELECT ServCat, Format(mid(ServCat , 2),'000000') AS SortingColumn, Number, Division, Coded, Reason, ServCatDesc " _
    & "FROM tblServCats " _
    & "WHERE Div = '" & vDiv & "' " _
    & "ORDER BY  Format(mid(ServCat , 2),'000000')"
0
 

Author Closing Comment

by:ScootterP
ID: 37778000
Beautiful.  Thanks.
0
 
LVL 75
ID: 37778024
You are welcome ...
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question