SQL - Converting 24-hour (“military”) time (2145) to “AM/PM time” (9:45 pm)

前端 未结 7 1166
一向
一向 2020-12-21 00:09

I have 2 fields I\'m working with that are stored as smallint military structured times.
Edit I\'m running on IBM Informix Dynamic Server Version 10.00.FC9

7条回答
  •  暖寄归人
    2020-12-21 00:47

    LONG-hand approach... but works

    select  substr((mtg_rec.beg_tm-1200),0,1)||":"||substr((mtg_rec.beg_tm-1200),2,2)||" pm" beg_tm,
                substr((mtg_rec.end_tm-1200),0,1)||":"||substr((mtg_rec.end_tm-1200),2,2)||" pm" end_tm
        from    mtg_rec
        where   mtg_rec.beg_tm between 1300 and 2159
                and mtg_rec.end_tm between 1300 and 2159
        union
        select  substr((mtg_rec.beg_tm-1200),0,1)||":"||substr((mtg_rec.beg_tm-1200),2,2)||" pm" beg_tm,
                substr((mtg_rec.end_tm-1200),0,2)||":"||substr((mtg_rec.end_tm-1200),3,2)||" pm" end_tm
        from    mtg_rec
        where   mtg_rec.beg_tm between 1300 and 2159
                and mtg_rec.end_tm between 2159 and 2400
        union
        select  substr((mtg_rec.beg_tm-1200),0,2)||":"||substr((mtg_rec.beg_tm-1200),3,2)||" pm" beg_tm,
                substr((mtg_rec.end_tm-1200),0,2)||":"||substr((mtg_rec.end_tm-1200),3,2)||" pm" end_tm
                mtg_rec.days
        from    mtg_rec
        where   mtg_rec.beg_tm between 2159 and 2400
                and mtg_rec.end_tm between 2159 and 2400
        union
         select substr((mtg_rec.beg_tm),0,1)||":"||(substr((mtg_rec.beg_tm),2,2))||" am" beg_tm,
                substr((mtg_rec.end_tm),0,1)||":"||(substr((mtg_rec.end_tm),2,2))||" am" end_tm
                mtg_rec.days
        from    mtg_rec
        where   mtg_rec.beg_tm between 0 and 959
                and mtg_rec.end_tm between 0 and 959
        union
         select substr((mtg_rec.beg_tm),0,2)||":"||(substr((mtg_rec.beg_tm),3,2))||" am" beg_tm,
                substr((mtg_rec.end_tm),0,2)||":"||(substr((mtg_rec.end_tm),3,2))||" am" end_tm
                mtg_rec.days
        from    mtg_rec
        where   mtg_rec.beg_tm between 1000 and 1259
                and mtg_rec.end_tm between 1000 and 1259
        union
         select cast(beg_tm as varchar(4)),
                cast(end_tm as varchar(4))
        from    mtg_rec
        where   mtg_rec.beg_tm = 0
                and mtg_rec.end_tm = 0
        into temp time_machine with no log;
    

提交回复
热议问题