Solved

VB6 SQL Order By (Number)

Posted on 2010-09-03
9
195 Views
Last Modified: 2012-05-10
Hey,

Im looking for how to correctly order by a number.
At the moment, im getting results like:
1
10
11
2
3
4
etc...

How do I do it correctly?

Thanks,
Jessee
0
Comment
Question by:Letsgetcoding
  • 5
  • 3
9 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33594281
How are these numbers stored ? (I assume as a string). In what do you want to sort ?
Can you show us some code where you perform the sorting ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33594288
If it's on sqlserver, try something like this

SELECT
 ...
ORDER BY
  CASE  
    WHEN ISNUMERIC(value) = 1 THEN CONVERT(INT, value)  
    ELSE 9999999 -- or something huge
  END,
  value
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 33594289
Sounds like the field is text and not numeric.  I believe an ORDER BY CAST(DesiredField AS Integer) should work if I'm right... but if you want a more confident answer, you'll have to post some code and explain the datatypes of the fields you're using.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:Letsgetcoding
ID: 33594368
Hey,

.CommandText = "SELECT * FROM tblSongs WHERE colPlaylist= ? ORDER BY colPlaylistPos"

colPlaylistPos has a text datatype.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33594376
Try the following

.CommandText = "SELECT * FROM tblSongs WHERE colPlaylist= ? ORDER BY CASE  WHEN ISNUMERIC(colPlaylistPos) = 1 THEN CONVERT(INT, colPlaylistPos)  ELSE 9999999 END"
0
 

Author Comment

by:Letsgetcoding
ID: 33594399
Method .execute of object _Command failed.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33594406
What database are you using ?
0
 

Author Comment

by:Letsgetcoding
ID: 33594414
Access
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 125 total points
ID: 33594517
.CommandText = "SELECT * FROM tblSongs WHERE colPlaylist= ? ORDERBY VAL(colPlaylistPos)  "
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question