HOW TO SORT NUMBER AND TEXT IN SAME FIELD?

G

Guest

I have created a database that has a column that contains both numbers and
letters. ex 10, 100a, 101a, 201a, 1000 etc. I would like this column to
sort 1,2,3 etc. - 100, 100a, 100b etc. How do I get it to sort in this
fashion?
 
B

Brendan Reynolds

If the letters always come after the numbers, as in your example, you could
sort using the Val() function. For example, in a query ...

SELECT * FROM YourTable ORDER BY Val([YourField])

This will not be very efficient, however, as the database engine will not be
able to use an index to speed up sorting. A more efficient approach would be
to use two fields, one number and one text. You can always concatenate the
two for display purposes ...

SELECT [YourNumberField] & [YourTextField] FROM YourTable ORDER BY
YourNumberField, YourTextField

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
J

Joseph Meehan

Doris said:
I have created a database that has a column that contains both numbers and
letters. ex 10, 100a, 101a, 201a, 1000 etc. I would like this column to
sort 1,2,3 etc. - 100, 100a, 100b etc. How do I get it to sort in this
fashion?

Just a few ideas that should help you do what you need.

First you don't have text and numbers in the same field. It appears you
have text. 1 or 2 can be text or numbers. If they are in a text field they
are text and will be treated as such.

It may be easiest to divide the data into two fields or use a query to
divide it up. Right now I can't recall the sort order that Access uses nor
do I remember if you can change it.
 
M

Mike Painter

Doris said:
I have created a database that has a column that contains both
numbers and letters. ex 10, 100a, 101a, 201a, 1000 etc. I would
like this column to sort 1,2,3 etc. - 100, 100a, 100b etc. How do I
get it to sort in this fashion?

Since you are using a text field you can only do this if the text line up

A query with a calculated field can be sorted on and the actual field
displayed.

SortField: Right ("000000000" & YourField, 10) will work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top