dbTalk Databases Forums  

Sorting a Text field like a Number Field

comp.database.ms-access comp.database.ms-access


Discuss Sorting a Text field like a Number Field in the comp.database.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Nathan Given
 
Posts: n/a

Default Sorting a Text field like a Number Field - 10-05-2003 , 06:31 PM






I have a table with around 18,000 records in it. One of the fields is
a Text field, but 85% of the things in that field is a number (The
other 15% is a number with a text suffix).

I was wondering if there was an easy way to Sort this field as if it
were a number (because, when it sorts as text it looks like this...

1
11
12
14
2
21
24
3
35


So far, the only thing I came up with was to make a query with this
expression...

TempSection: CDbl([Section])

But that either produces a #Error for those rows where the Section has
a text character in there or it won't even open the query in datasheet
view because of Data type mismatch in criteria expression.

What can I do?

Can I create an iif statement to catch that error? or Something?

Thanks!
--
Nathan

Reply With Quote
  #2  
Old   
Ira Solomon
 
Posts: n/a

Default Re: Sorting a Text field like a Number Field - 10-06-2003 , 09:04 AM






Try this:
sc:iif(isnumeric(x!y),cdbl(x!y),0)

where X is table and Y is field.

Sort on this but display y.

This will place all fields containing text at the top and then sort
numbers. If you want all text fields at the bottom then change 0 to
99999999.

If you really need to sort the text contianing fields as well you will
need a funtion to extract the numeric part and use that as the
arguement to cdbl. You wouldn't need the iif function then.

Good Luck
Ira Solomon

On 5 Oct 2003 16:31:25 -0700, ngiven (AT) hotmail (DOT) com (Nathan Given) wrote:

Quote:
I have a table with around 18,000 records in it. One of the fields is
a Text field, but 85% of the things in that field is a number (The
other 15% is a number with a text suffix).

I was wondering if there was an easy way to Sort this field as if it
were a number (because, when it sorts as text it looks like this...

1
11
12
14
2
21
24
3
35


So far, the only thing I came up with was to make a query with this
expression...

TempSection: CDbl([Section])

But that either produces a #Error for those rows where the Section has
a text character in there or it won't even open the query in datasheet
view because of Data type mismatch in criteria expression.

What can I do?

Can I create an iif statement to catch that error? or Something?

Thanks!


Reply With Quote
  #3  
Old   
Philippa
 
Posts: n/a

Default Re: Sorting a Text field like a Number Field - 10-20-2003 , 09:00 AM



in the query pane where the fieldname is overtype with: cint(
fieldname ). This will convert the field into a interger, so when
sorted, it appears in the correct order.

if there are also letter charactors stored in that field in a normal
SQL environment you can use a case statement: something like

case when isnumeric(fieldname) = true then cint(fieldname) else
fieldname end

But unfortuantely access sql, insn't ansi standard so its not that
good.

I hope this helps....
Philippa

ngiven (AT) hotmail (DOT) com (Nathan Given) wrote in message news:<f749d926.0310051531.a1f1a3d (AT) posting (DOT) google.com>...
Quote:
I have a table with around 18,000 records in it. One of the fields is
a Text field, but 85% of the things in that field is a number (The
other 15% is a number with a text suffix).

I was wondering if there was an easy way to Sort this field as if it
were a number (because, when it sorts as text it looks like this...

1
11
12
14
2
21
24
3
35


So far, the only thing I came up with was to make a query with this
expression...

TempSection: CDbl([Section])

But that either produces a #Error for those rows where the Section has
a text character in there or it won't even open the query in datasheet
view because of Data type mismatch in criteria expression.

What can I do?

Can I create an iif statement to catch that error? or Something?

Thanks!

Reply With Quote
  #4  
Old   
John
 
Posts: n/a

Default Re: Sorting a Text field like a Number Field - 10-28-2003 , 03:12 AM



Should be a simple matter of ORDER BY val(strField) if I'm
understanding your problem correctly... but it's late.

to test this create a table (tblStrNum) with 2 fields
(str:text,num:number)
open the table and enter 1,22text,100 into str. now enter 1,22,100
into num.

create a query (qrySortStrNum):

Select str,num
FROM tblStrNum
ORDER BY str;

str num
1 1
100 100
22text 22

this represents your current situation. now modify the sql to read:

Select str,num
FROM tblStrNum
ORDER BY val(str);

result:

str num
1 1
22text 22
100 100

and thereyago :-)

-John

ngiven (AT) hotmail (DOT) com (Nathan Given) wrote in message news:<f749d926.0310051531.a1f1a3d (AT) posting (DOT) google.com>...
Quote:
I have a table with around 18,000 records in it. One of the fields is
a Text field, but 85% of the things in that field is a number (The
other 15% is a number with a text suffix).

I was wondering if there was an easy way to Sort this field as if it
were a number (because, when it sorts as text it looks like this...

1
11
12
14
2
21
24
3
35


So far, the only thing I came up with was to make a query with this
expression...

TempSection: CDbl([Section])

But that either produces a #Error for those rows where the Section has
a text character in there or it won't even open the query in datasheet
view because of Data type mismatch in criteria expression.

What can I do?

Can I create an iif statement to catch that error? or Something?

Thanks!

Reply With Quote
  #5  
Old   
Bruce Pick
 
Posts: n/a

Default Re: Sorting a Text field like a Number Field - 01-21-2004 , 04:20 PM



I did it this way. In my case, I had street numbers [STREET_NUMBER]
entered as text. None were > 999, but some had non-numerical junk, or
nulls.
This method left records with unusual content unmodified, and useful
results that could be sorted nicely. You could expand the code to cover
longer strings.

STREET_NUM: IIf(Len([STREET_NUMBER])=1," " &
STREET_NUMBER],IIf(Len([STREET_NUMBER])=2," " &
[STREET_NUMBER],[STREET_NUMBER]))

John wrote:

Quote:
Should be a simple matter of ORDER BY val(strField) if I'm
understanding your problem correctly... but it's late.

to test this create a table (tblStrNum) with 2 fields
(str:text,num:number)
open the table and enter 1,22text,100 into str. now enter 1,22,100
into num.

create a query (qrySortStrNum):

Select str,num
FROM tblStrNum
ORDER BY str;

str num
1 1
100 100
22text 22

this represents your current situation. now modify the sql to read:

Select str,num
FROM tblStrNum
ORDER BY val(str);

result:

str num
1 1
22text 22
100 100

and thereyago :-)

-John

ngiven (AT) hotmail (DOT) com (Nathan Given) wrote in message news:<f749d926.0310051531.a1f1a3d (AT) posting (DOT) google.com>...

I have a table with around 18,000 records in it. One of the fields is
a Text field, but 85% of the things in that field is a number (The
other 15% is a number with a text suffix).

I was wondering if there was an easy way to Sort this field as if it
were a number (because, when it sorts as text it looks like this...

1
11
12
14
2
21
24
3
35


So far, the only thing I came up with was to make a query with this
expression...

TempSection: CDbl([Section])

But that either produces a #Error for those rows where the Section has
a text character in there or it won't even open the query in datasheet
view because of Data type mismatch in criteria expression.

What can I do?

Can I create an iif statement to catch that error? or Something?

Thanks!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.