tags:

views:

81

answers:

3

Query:

SELECT StartDate, EndDate, RIGHT(Sector, 1 )
FROM Table1
ORDER BY Right(Sector, 1), StartDate

By looking at this, the query should order everything by sector, followed by the start date. This query has worked for quiet awhile until yesterday where it did not order it properly, for some reason, Sector 2 came before Sector 1.

The data type for Sector is of type int, not null. After inserting a TRIM function into Sector it seems to work fine afterwards.

New Query:

SELECT StartDate, EndDate, RIGHT(Sector, 1 )
FROM Table1
ORDER BY Right(TRIM(Sector), 1), StartDate

Which I found really weird since it's suppose to only pick out one character, so why is there leading spaces?

Is there an issue with using RIGHT function on a int before converting the type? Or is it something else?

Thanks for the help everyone!

-Edit- The RIGHT function should return either 1,2,3 or 4 however when ordering it, 2 comes before 1.

To clarify, the column Sector contains an int value, we can determine it's location by obtaining the last digit (Which is why the previous coder did)

+3  A: 

MS Access 2003 has a curious little feature (I can't speak for the other versions):

  1. Make a simple query. Sort by Column A Ascending. Save the query.
  2. Run the query. When you see the output, sort by Column A Descending using the toolbar option (see pic below). Save & close.
  3. Run the query again. Your new sort will have overridden the sort that you saved in the query.

I think you or someone else probably just opened the query out of curiosity, sorted by Sector Descending, and when prompted to save Design Changes, you chose Yes (even though technically you didn't make any). The easiest way I found to restore the original sort is to edit the query and save it.

alt text

PowerUser
+1, That's quite interesting to know! Though I've created and ran both my queries without modifying the results and ended up with the same errors as stated (TRIm => ordering is correct, No TRIM => no proper ordering)
MikeAbyss
I've noticed this when changing the column order as well.
Jeff O
Possibly the most correct answer, after deleting the query and creating the two again I can't replicate the issue. Thanks PowerUser!
MikeAbyss
Glad to be of service. It's one of those things in Access that looks like a feature from one point of view, and a bug from another point of view.
PowerUser
+1  A: 

It seems pretty obvious that you have a blank space at the end of the Sector field that the trim is removing.

wcm
Maybe obvious is a bit of a strong word ... Possible maybe?
wcm
RIGHT(Sector, 1 ) would return a space. It's possible, he missed that.
Jeff O
The OP said Sector is integer. How can it include a blank space at the end?
HansUp
All that the OP is really doing by invoking trim() is re-instating the original sort order. i.e., it has nothing to do with blank spaces, especially since Sector is an integer.
PowerUser
@HansUp, you are right that he did indicate it is an int when he edited the question. I guess I keep thinking that it's a string because it doesn't make sense to me that the Right function is being used at all. If Sector is an int, why not just sort on Sector MOD 10.
wcm
@wcm Something about `Right(IntegerValue, 1)` just seems wrong to me. I prefer `Sector MOD 10` as you suggested. Add that suggestion to your answer and you'll maybe get some upvotes. At least one, from me.
HansUp
+3  A: 

You've got your data stored wrong if you need to sort on a subcharacter of a numeric field.

That said, in certain context, VBA functions reserve a space in string representations of numbers for the sign. A nonsensical example of this would be:

  ?Len("12345")
   5 

Notice the space at the beginning (where the - would be if the number returned by Len() could be negative). I thought this was a result of coercing a number to a string value, but that's not it, and I couldn't replicate the problem. But that would likely be the source of the problem, and, of course, trimming off the leading space would take care of the issue.

But that's two function calls for each line, and then you're sorting by it, and that means no use of indexes, so it's going to be slow relative to a SORT BY that can use indexes. So, I'd conclude you have a schema error, in that you're giving meaning to a subpart of the data stored in the field.

David-W-Fenton
+1 Interesting post, indeed this sounds real close to the issue I was encountering and how trim fixed it.
MikeAbyss