You comparisons are done based on strings.
For example, which should come first, 9 or 10? If you compare them as a number, the 9 should come first, but if you compare them as strings, the 10 would come first.
Copy/paste this to a query window and run it.
Code:
Declare @Temp Table(Data VarChar(10))
Insert Into @Temp Values('9')
Insert Into @Temp Values('10')
Select * From @Temp Order By Data
String comparisons (and ordering) is actually based on collations within SQL Server. Collations are a relatively large and somewhat complicated topic. Basically, though, you can simplify this to be.... ASCII sorting.
With string sorting, each character is sorted separately from left to right. In the 9 versus 10 issue (when sorting as a string), the first characters are sorted so the 9 is sorted relative to the 1 (from the 10). Then the second characters is sorted. This is no second character for the 9, but there is for the 10. No character sorts before any character. With string sorts, characters sort AFTER numbers. Expanding on the earlier example...
Code:
Declare @Temp Table(Data VarChar(10))
Insert Into @Temp Values('9')
Insert Into @Temp Values('10')
Insert Into @Temp Values('a')
Insert Into @Temp Values('1')
Insert Into @Temp Values('100')
Select * From @Temp Order By Data
Results:
[tt][blue]
Data
----
1
10
100
9
a
[/blue][/tt]
I hope the "WHY" part of your question is answered. Now, on to the "How do I" part. I recommend that you create another table in your database. This table should contain exactly one row for each distinct relname/version number. This table will require a bit of maintenance from time to time. Specifically, when you create a new version, you'll need to add a new row to this table. By adding this table, you will suddenly make your life a lot easier, more flexible, and probably faster too.
To create the table....
Code:
Create Table VersionNumbers(Name VarChar(20), AnotherColumn VarChar(20))
Insert Into VersionNumbers(Name) Select Distinct Name From [table]
This is just a brief mock up. You'll want to make sure you have an index on the name column in this table. In fact, you'll probably want this to be a primary key.
Anyway.... after you create the table, you can put any value you want in for the "AnotherColumn" column (Here, Here2, Here3, etc...)
Then, all of the code you had before would become this:
Code:
SELECT [table].[Name], VersionNumbers.AnotherColumn
FROM [Table]
Left Join VersionNumbers
On [table].[name] = VersionNumbers.[name]
WHERE [table].NameID = 3956
Once you have this table, you will likely find lots of uses for it. For example, you could create another column for the "Major Version Number". This column would probably be an integer. You could create another column for "Minor Version Number", Windows Operating System, Release Date, etc.... Basically, this new table will remove the burden of trying to make meaningful sense of out a column that appears to be storing (at least) 3 separate pieces of information.
Make sense?
-
George
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom