tags:

views:

44

answers:

2

I want to select * from tbl order by characters, for example:

a)   1 coloum has          a
b)   1 coluum has          ab

I want it to place ab first and then a

+4  A: 

In SQL Server:

select * from tbl 
order by len(col) desc

where tbl is your table and col is the column.

This returns the results ordered by the length of col, longest first.

Blorgbeard
I believe this doesn't use indexes since you are calculating the value. It would be significantly slower than a regular order. If it's too slow, maybe you can precalculate the length (in your code, stored procedure or trigger) and store it alongside the value.
Nelson
It won't use indexes anyway, since there's no where clause. The whole table needs to be scanned and sorted. It's true that precalculating the length would be quicker, but I wouldn't assume that it would be a significant enough difference to go to the trouble - without benchmarking anyway.
Blorgbeard
In SQL Server you can make an index on a persisted computed column, but that will just mean that the scan is already pre-ordered (without a where clause).
Cade Roux
+1  A: 

Sql takes care of length of the coleumn, simply select * from tbl order by col desc

TonyP