how to convert weird varchar “time” to real time in mysql?

前端 未结 5 748

I have a time value being stored in a database as a varchar(4) and I need to convert it to real time.

for example, if the time is \"23:59\" I want 11:59PM returned.

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 12:20

    Using a table named test with a column named string and with values

    2300
    2100
    1200
    0430
    0430
    

    With query

    select concat(time_format(concat(substring(string,-4,2),':',substring(string,3)),'%h:%i'),
       case when string >= 1200 then ' PM' else ' AM' end)
    from test;
    

    You get,

    11:00 PM
    09:00 PM
    12:00 PM
    04:30 AM
    04:30 AM
    

提交回复
热议问题