E
E
eldar_web2017-08-14 16:42:32
MySQL
eldar_web, 2017-08-14 16:42:32

How to sort alphabetically in SQL excluding extra words?

Let's say there is an Address table with name fields.
Such records:
Belova
st Aleksey
st Fadeev st
Due to the fact that there is a 'st' at the beginning, the usual alphabetical sorting ORDER BY name ASC does not work.
How to solve the problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
Boris Korobkov, 2017-08-14
@BorisKorobkov

If you need to sort so that "Belova street" and "Belova passage" are nearby, then ...
option 1: call them "Belova street" and "Belova passage".
option 2: store in separate fields "street" + "Belova" and "passage" + "Belova". And sort by second

E
Eugene, 2017-08-14
@Flashter

It's strange that it doesn't work, because sorting alphabetically means sorting by 4 characters if the first three are the same...

X
x67, 2017-08-14
@x67

Bring to a general view the format of the street.
To be st. Academician and there was no "Academician's street", "Academician's street" and so on. In pgsql, you can create separate functions for this, using a whole bunch of built-in functions for working with text. I don’t know how to do this in mysql, but I would very quickly implement it for myself in python using formatting and regular expressions. After that, you can sort. If there are not only streets, separate the address into two fields. 1st field - type (street, highway, lane, etc.), 2nd field - name (Akademika Yangelya, Electrodnaya, Parkovy). Thus, you can sort this table as you like.

K
Konstantin Tsvetkov, 2017-08-14
@tsklab

Due to the fact that there is a 'st' at the beginning, the usual alphabetical sorting ORDER BY name ASC does not work. How to solve the problem?
In the forehead: remove from the title "excluding unnecessary words" and sort. Either add or calculate a custom sort field.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question