Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Syntax

Posted on 2012-03-28
5
Medium Priority
?
350 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:Scott Palmer
  • 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 - Microsoft MVP, Access and Data Platform) earned 2000 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:Scott Palmer
ID: 37778000
Beautiful.  Thanks.
0
 
LVL 75
ID: 37778024
You are welcome ...
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

926 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