Solved

SQL Syntax

Posted on 2012-03-28
5
324 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
Comment Utility
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
Comment Utility
Try this

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

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
Beautiful.  Thanks.
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
You are welcome ...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now