A query to sort the fields so that the rows are first and then the numbers

I have a column that contains numbers or rows. The column type is varchar.

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

TIA!

+3
source share
4 answers

You will need to write it in two separate queries. One for selecting numbers, the other for strings. I would like to create a second column (one for numbers, one for rows), which will simplify and speed up the execution of these two queries.

+1
source

It worked for me ...

Select * from Table order by stringfield+0;

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

: ....

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

( , ):

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

The first choice should return only values ​​that are not numeric, while the second should return only values ​​that are numeric.

For a table containing a mixture of numeric and string data, a string is first displayed, then a number.

0
source

Have you considered creating a custom sort function ? I never used it myself, but it sounds exactly the way you need it.

0
source

Source: https://habr.com/ru/post/1752467/


All Articles