Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Sorting a Query or Stored Procedure by Using Only a Portion of a Field's Value


View products that this article applies to.

This article was previously published under Q209821
Novice: Requires knowledge of the user interface on single-user computers.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

↑ Back to the top


Summary

You can sort or group a query or stored procedure by using only a portion of a field's value. This technique is useful if a field contains values with a set number of leading or trailing characters that are insignificant to your sorting task. An example of this might be a part number field that contained the following values:
A-453-34567
A-123-45675
B-234-75658
B-645-65759

↑ Back to the top


More information

In a Microsoft Access Database (.mdb)

To demonstrate how to sort or group a query by using only a portion of a field's value, this article uses the sample database Northwind.mdb to create a query based on the Employees table. The query uses the Mid() function to sort by six characters beginning with the fourth character in the LastName field. You could also use the Left() and Right() functions to return characters from the left or from the right side of a string argument.

NOTE: You can see a demonstration of the technique that is used in this article in the sample file Qrysmp00.exe. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:
207626� ACC2000: Access 2000 Sample Queries Available in Download Center
The following procedure explains how to use the Mid() function to sort data by using only a portion of a field's value:
  1. Open the sample database Northwind.mdb.
  2. Create a new query based on the Employees table.
  3. Enter the following line in the Field row in the first column of the query grid:
    Sort String: Mid([LastName],4,6)
  4. Select the Sort cell in the Sort String column and select Ascending or Descending.
  5. Drag the LastName field from the Employees box to the QBE grid.
  6. Switch the query to Datasheet view. Note that Sort String appears as the name of the first column, but you can change this header to any other name. Note also that the employee records are sorted by the fourth letters of the employees' last names.
If you wanted to use this technique on the part number example

A-453-34567
A-123-45675
B-234-75658
B-645-65759
you could sort these part numbers by the third through fifth characters only by using the following expression:
Expr1: Mid([PartNo]),3,3)

In a Microsoft Access Project (.adp)

To demonstrate how to sort or group a stored procedure by using only a portion of a field's value, this article uses the sample project NorthwindCS.adp to create a stored procedure based on the Employees table. The stored procedure uses the SubString() function to sort by six characters beginning with the fourth character in the LastName field. You could also use the Left() and Right() functions to return characters from the left or from the right side of a string argument.

  1. Open the sample project NorthwindCS.adp.
  2. Create the following stored procedure:
    CREATE PROCEDURE "SortString_Proc"
    
    AS
    
       SELECT SUBSTRING(LastName,4,6) AS [Sort String], LastName
       FROM Employees
       ORDER BY (SUBSTRING(LastName,4,6))
    					
  3. Save the stored procedure and switch to Datasheet view. Note that Sort String appears as the name of the first column, but you can change this header to any other name. Note also that the employee records are sorted by the fourth letters of the employees' last names.

↑ Back to the top


References

For more information about string expressions, click Microsoft Access Help on the Help menu, type examples of expressions in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB209821, kbhowto

↑ Back to the top

Article Info
Article ID : 209821
Revision : 3
Created on : 6/28/2004
Published on : 6/28/2004
Exists online : False
Views : 386