Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL Column Year Parsing and Sorting

I have one table, I want to sort by years. However, we have a problem.

The column is in text format. Because it is in the years before Christ.

First I need to list the years before Christ and then the present years.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you for your help in this matter.

Table: date_events

event_year event_title
B.C 405 example
B.C 406 example
2022 example

>Solution :

You can order by the column event_year, but replacing 'B.C ' by a minus sign '-', and converting it to a signed:

select *
from date_events
order by cast(replace(event_year, 'B.C ', '-') as signed);

Output:

event_year event_title
B.C 406 example
B.C 405 example
2022 example

Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading