Numeric Combo Box Sort

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I use the following SQL:

SELECT tblProfilesStorage.StorageTime
FROM tblProfilesStorage
GROUP BY tblProfilesStorage.StorageTime, Right(" " & [StorageTime],4)
ORDER BY Right(" " & [StorageTime],4);

This works fine EXCEPT when there's a value with a decimal point. For
example, 6.5 will come after 599 instead of 6.

How can I revise this code to sort properly?

Thanks!!!
 
I use the following SQL:

SELECT tblProfilesStorage.StorageTime
FROM tblProfilesStorage
GROUP BY tblProfilesStorage.StorageTime, Right(" " & [StorageTime],4)
ORDER BY Right(" " & [StorageTime],4);

This works fine EXCEPT when there's a value with a decimal point. For
example, 6.5 will come after 599 instead of 6.

How can I revise this code to sort properly?

Thanks!!!

First of all, I can't see any reason to use a GROUP BY clause here.
You are selecting only one column out of your table. Although it is
syntactically permissible, GROUP BY only makes sense if you are
calculating sums or using some other aggregate function for multiple
rows containing the same value in the column to group by. If the table
contains duplicate values in that column, you can use SELECT DISTINCT
instead.

Second, in order to sort numerically, you need to ORDER BY the column
StorageTime without any of the formatting (assuming that StorageTime
is some numeric type); otherwise, if that column is stored as Text in
the table, you'll need to convert it to some numeric value, e.g. using
the CDbl() function.

Third, you aren't including the formatted StorageTime in your output
column list, so what good is it?

Fourth, if you do include it in your output list, you should give it
an alias so that Access doesn't show a column name such as "Expr1234".

Assuming that StorageTime is stored numerically, I would suggest the
following (incidentally, there is no need to prefix the field with the
table name if you are selecting from one single table):

SELECT StorageTime, Right(" " & StorageTime],4)
AS [Pretty Storage Time]
FROM tblProfilesStorage
ORDER BY StorageTime;

If you are storing StorageTime as Text for some reason, then you will
need to do something like the following:

SELECT StorageTime, Right(" " & StorageTime],4)
AS [Pretty Storage Time]
FROM tblProfilesStorage
ORDER BY CDbl(Nz([StorageTime],"0"));

The Nz() function should keep you from getting an error when
StorageTime contains null values.

Good luck!
 
WOW! Thanks for all of the points. I've had to make some adjustments to what
you posted (syntax error, etc.) and arrived at this:
SELECT DISTINCT tblProfilesStorage.StorageTime, Right(" " &
[StorageTime],4) AS [Pretty Storage Time]
FROM tblProfilesStorage
ORDER BY tblProfilesStorage.StorageTime;

This still doesn't sort properly:
1
10
10.5
12
120
130
14
15
150
18
180

etc. Can you see what's wrong...?

--
www.Marzetti.com


Bob Hairgrove said:
I use the following SQL:

SELECT tblProfilesStorage.StorageTime
FROM tblProfilesStorage
GROUP BY tblProfilesStorage.StorageTime, Right(" " & [StorageTime],4)
ORDER BY Right(" " & [StorageTime],4);

This works fine EXCEPT when there's a value with a decimal point. For
example, 6.5 will come after 599 instead of 6.

How can I revise this code to sort properly?

Thanks!!!

First of all, I can't see any reason to use a GROUP BY clause here.
You are selecting only one column out of your table. Although it is
syntactically permissible, GROUP BY only makes sense if you are
calculating sums or using some other aggregate function for multiple
rows containing the same value in the column to group by. If the table
contains duplicate values in that column, you can use SELECT DISTINCT
instead.

Second, in order to sort numerically, you need to ORDER BY the column
StorageTime without any of the formatting (assuming that StorageTime
is some numeric type); otherwise, if that column is stored as Text in
the table, you'll need to convert it to some numeric value, e.g. using
the CDbl() function.

Third, you aren't including the formatted StorageTime in your output
column list, so what good is it?

Fourth, if you do include it in your output list, you should give it
an alias so that Access doesn't show a column name such as "Expr1234".

Assuming that StorageTime is stored numerically, I would suggest the
following (incidentally, there is no need to prefix the field with the
table name if you are selecting from one single table):

SELECT StorageTime, Right(" " & StorageTime],4)
AS [Pretty Storage Time]
FROM tblProfilesStorage
ORDER BY StorageTime;

If you are storing StorageTime as Text for some reason, then you will
need to do something like the following:

SELECT StorageTime, Right(" " & StorageTime],4)
AS [Pretty Storage Time]
FROM tblProfilesStorage
ORDER BY CDbl(Nz([StorageTime],"0"));

The Nz() function should keep you from getting an error when
StorageTime contains null values.

Good luck!
 
WOW! Thanks for all of the points. I've had to make some adjustments to what
you posted (syntax error, etc.) and arrived at this:
SELECT DISTINCT tblProfilesStorage.StorageTime, Right(" " &
[StorageTime],4) AS [Pretty Storage Time]
FROM tblProfilesStorage
ORDER BY tblProfilesStorage.StorageTime;

This still doesn't sort properly:
1
10
10.5
12
120
130
14
15
150
18
180

etc. Can you see what's wrong...?

Looks like you are storing StorageTime in the table as Text after all.
Otherwise, I can't see why it wouldn't work. Open the table in design
view and check the data type of that column.
 
VERY GOOD! My oversight! I could've sworn it was stored as Numeric. I changed
it from Text and it's fine.

Thanks for all of the great details - I learned a lot!

--
www.Marzetti.com


Bob Hairgrove said:
WOW! Thanks for all of the points. I've had to make some adjustments to what
you posted (syntax error, etc.) and arrived at this:
SELECT DISTINCT tblProfilesStorage.StorageTime, Right(" " &
[StorageTime],4) AS [Pretty Storage Time]
FROM tblProfilesStorage
ORDER BY tblProfilesStorage.StorageTime;

This still doesn't sort properly:
1
10
10.5
12
120
130
14
15
150
18
180

etc. Can you see what's wrong...?

Looks like you are storing StorageTime in the table as Text after all.
Otherwise, I can't see why it wouldn't work. Open the table in design
view and check the data type of that column.
 

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

Back
Top