0

Hi I have a table data_table .I need to get all the title from the table.The title field (type varchar) contain alphanumeric data like below

title
---------
ab 2007
ab 2017
ba 2018
ba 2017
cd 2017
cd 2016

My desired output is

 title
---------
 ba 2018
 ab 2017
 ba 2017
 cd 2017
 cd 2016
 ab 2007

I tried by CAST .But didnt get required result

SELECT title, CAST(title as SIGNED) AS casted_column FROM data_table ORDER BY `title` DESC
1
  • 1
    Secretly, I think you know what to do Commented Aug 21, 2017 at 6:41

2 Answers 2

1

Use RIGHT to get last 4 digits and then cast it to unsigned and sort it

SELECT title 
from tbl
order by cast(right(title,4) as unsigned) desc, title desc
Sign up to request clarification or add additional context in comments.

Comments

1

if your title end always with 4 digit you could substr for obtain the number and use in sort

      SELECT title
      FROM data_table
      ORDER BY substr(`title`, -4) DESC , title DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.