views:

62

answers:

4

I've a column in which contains numbers or strings. The type of the column is varchar.

Usually when we sort it using the string field, then all the numbers come first and then strings start. But I want all the strings first and then numbers.

TIA !

A: 

This worked for me...

Select * from Table order by stringfield+0;

edit: http://www.sqlite.org/datatypes.html (Point 4.0)

UPDATE: Try this....

select * from Table where LENGTH(trim(stringfield,"0123456789 ") )=0 union select * from table order by stringfield;
st0le
But in this case, consider if you have the records as 123 and 43 then you will get the result as 43 and then 123. It should be 123 then 43.
Karan
I've updated my answer, please try it out and let me know if it satisfies your requirement...
st0le
Its not working. Even in the second case all the numbers come at start.
Karan
+1  A: 

You'll have to write it in two separate queries. One for selecting numbers, the other for strings. Preferably I would create a second column (one for numbers, one for strings), making it easier and faster to have those two queries run.

Peter
A: 

How about the following (two queries as suggested above):

select * from Table where LENGTH(trim(stringfield,"0123456789 ")) > 0; select * from table where LENGTH(trim(stringfield,"0123456789 ")) = 0;

The first select should return only values that are not numeric, whilst the second should return only values that are numeric.

For a table that contains a mixture of numeric and string data, this outputs the strings first, then the numbers.

Gordon Mckeown
A: 

Have you considered creating a custom collation-function? I have never used this myself, but it sounds like exactly what you need.

Space_C0wb0y