Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following SQL:

;WITH times AS (
    SELECT '0830' AS t
    SELECT '0930' AS t
    SELECT '1030' AS t
    SELECT '1130' AS t
    SELECT '1230' AS t
SELECT t as time
FROM times

When I execute this in SSMS (SQL Server Management Studio), it displayed correctly as


However, when I put this into a drop down list inside Visual Studio 2010, the list becomes incorrectly sorted:


The 0 is disappearing from the 0830 and 0930 and both 0830 to 0930 is being shifted from the 1st two items in the list to the last two items.

How can I make the drop-down show the list in the correct order?

share|improve this question

closed as off topic by Jon Seigel, RolandoMySQLDBA, Derek Downey, Leigh Riffel, Marian Oct 10 '12 at 12:36

Questions on Database Administrators Stack Exchange are expected to relate to database administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Your application is sorting it based on the character values (i.e. a string sort, as opposed to a numeric one). If SQL Server is handing the application the data correctly, then this sounds like an application issue. – Thomas Stringer Sep 27 '12 at 3:09
How about ORDER BY CONVERT(INT, time)? Or using a TIME data type instead of a string? – Aaron Bertrand Sep 27 '12 at 3:26
Despite the lack of an ORDER BY, this is an application issue, probably something to do with data types and how the list is getting populated. – Jon Seigel Sep 27 '12 at 3:26
While the solution seems to have been simple enough, I wonder if the difference could be a plan issue based on different session settings (e.g. the same kind of issue that often causes an app to be slow but SSMS to be fast). – Aaron Bertrand Sep 27 '12 at 12:19
up vote 1 down vote accepted

Specials thanks to Shark, Aaron Bertrand and Jon Seigel that have enlighten me.

I have use the ORDER BY CONVERT(INT, time) and it now solved the application issue problem. (Fortunately, the change to the SQL statement was a quick way to solve this problem)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.