Given the followingDECLARE @TABLE TABLE(descr VARCHAR(100))INSERT INTO @TABLESELECT 'Jo'UNION SELECT 'Blank'UNION SELECT 'Group 1'UNION SELECT 'Group 2'UNION SELECT 'Group 10'
I want output that makes sense to a human, so the order I would expect if I was to order it by hand would beBlankGroup 1Group 2Group 10JoSELECT * FROM @TABLESELECT * FROM @TABLE ORDER BY descr
Obviously dont quite do the trick!So, I have google'd and tried the followingDECLARE @maxlength INTEGERSELECT @maxlength = NULLSELECT @maxlength = ISNULL(MAX(LEN([descr])),0)FROM @TABLESELECT *, RIGHT(REPLICATE('0', @maxlength) + [descr], @maxlength)FROM @TABLEORDER BY RIGHT(REPLICATE('0', @maxlength) + [descr], @maxlength)Which gets the "Group" section ordering OK, but still is not really right.I struggle with this occassionally and have posted similar in the past and have always managed to ignore the problem (or implement a positional system, e.g. give each entry a position and allow them to be moved around, which is a bit clumsy).But it has bitten me again and so if anyone can explain a way to get the order working successfully regardless of what is in the table it would be great