开发者

Query to sort a field such that strings comes first and then numbers

开发者 https://www.devze.com 2023-01-05 18:40 出处:网络
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 w

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 开发者_Go百科strings first and then numbers.

TIA !


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.


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;


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消