SQL ordering only after the third character

5

I have a field in my MySQL table that is of type string, with the following data:

Coluna
01DV
03DV
04DV

If you enter the value 02CA the farm field thus

Coluna
01DV
02CA
03DV
04DV

I need it to look like this:

Coluna
02CA
01DV
03DV
04DV

How do I do this in MySQL?

    
asked by anonymous 03.07.2014 / 01:28

3 answers

6

If you want to sort first by the letters in the example, but keep the numerical order:

SELECT * FROM cadastro ORDER BY SUBSTRING(nome, 3), nome

That pretty much gives the same thing as

SELECT * FROM cadastro ORDER BY SUBSTRING(nome, 3), SUBSTRING(nome, 1, 2)

The syntax of substring is: substring( caractere inicial, quantidade ) .

    
03.07.2014 / 01:53
4

Like this:

SELECT coluna
FROM (SELECT coluna, substring(coluna, 3) coluna1 FROM tabela4) AS T 
ORDER BY T.coluna1, T.coluna

Example: SQLFiddle

    
03.07.2014 / 01:52
0

I was just able to make use of substring combined with order by as in the example below.

SELECT * FROM cadastro WHERE 1 order by SUBSTRING(nome,5)

In this way he sorts after the 5th character.

    
03.07.2014 / 01:43