<p>I have a column containing the strings 'Operator (1)' and so on until 'Operator (600)' so far.</p>

<p>I want to get them numerically ordered and I've come up with</p>

<pre><code>select colname from table order by 
cast(replace(replace(colname,'Operator (',''),')','') as int)
</code></pre>

<p>which is very very ugly.</p>

<p>Better suggestions?</p>

[{'answer_id': 75398, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 3, 'selected': True, 'text': "<p>It's that, InStr()/SubString(), changing Operator(1) to Operator(001), storing the n in Operator(n) separately, or creating a computed column that hides the ugly string manipulation.  What you have seems fine.</p>\n"}, {'answer_id': 75474, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>My answer would be to change the problem.  I would add an operatorNumber field to the table if that is possible.  Change the update/insert routines to extract the number and store it.  That way the string conversion hit is only once per record.</p>\n\n<p>The ordering logic would require the string conversion every time the query is run.</p>\n'}, {'answer_id': 76471, 'author': 'Ricardo C', 'author_id': 232589, 'author_profile': 'https://Stackoverflow.com/users/232589', 'pm_score': 0, 'selected': False, 'text': '<p>Well, first define the meaning of that column. Is operator a name so you can justify using chars? Or is it a number? </p>\n\n<p>If the field is a name then you will use chars, and then you would want to determine the fixed length. Pad all operator names with zeros on the left. Define naming rules for operators (I.E. No leters. Or the codes you would use in a series like "A001")</p>\n\n<p>An index will sort the physical data in the server. And a properly define text naming will sort them on a query. You would want both.</p>\n\n<p>If the operator is a number, then you got the data type for that column wrong and needs to be changed.</p>\n'}, {'answer_id': 76618, 'author': 'Cruachan', 'author_id': 7315, 'author_profile': 'https://Stackoverflow.com/users/7315', 'pm_score': 1, 'selected': False, 'text': "<p>If you really <em>have</em> to leave the data in the format you have - and adding a numeric sort order column is the better solution - then consider wrapping the text manipulation up in a user defined function.  </p>\n\n<p>select colname from table order by dbo.udfSortOperator(colname)</p>\n\n<p>It's less ugly and gives you some abstraction.  There's an additional overhead of the function call but on a table containing low thousands of rows in a not-too-heavily hit database server it's not a major concern.  Make notes in the function to optomise later as required.</p>\n"}, {'answer_id': 150106, 'author': 'Constantin', 'author_id': 20310, 'author_profile': 'https://Stackoverflow.com/users/20310', 'pm_score': 0, 'selected': False, 'text': "<p><strong>Indexed computed column</strong></p>\n\n<p>If you find yourself ordering on or otherwise querying <code>operator</code> column often, consider creating a computed column for its numeric value and adding an index for it. This will give you a computed/persistent column (which sounds like oxymoron, but isn't).</p>\n"}]
['https://Stackoverflow.com/questions/75361', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5190/']