Home > Enterprise >  MySQL Column Year Parsing and Sorting
MySQL Column Year Parsing and Sorting

Time:02-22

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.

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

CodePudding user response:

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

  • Related