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

前端 未结 7 1124
一向
一向 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 01:03

    Ah, a fellow Jenzabar user (Jonathan, don't be too cruel about the schemas. They are literally decades old). Surprised you didn't ask this on the CX-Tech list. I'd've sent you an RCS-ready stored procedure for CX.

    -sw

    {
     Revision Information (Automatically maintained by 'make' - DON'T CHANGE)
     -------------------------------------------------------------------------
     $Header$
     -------------------------------------------------------------------------
    }
    procedure       se_get_inttime
    privilege       owner
    description     "Get time from an integer field and return as datetime"
    inputs          param_time integer      "Integer formatted time"
    returns         datetime hour to minute "Time in datetime format"
    notes           "Get time from an integer field and return as datetime"
    
    begin procedure
    
    DEFINE tm_str VARCHAR(255);
    DEFINE h INTEGER;
    DEFINE m INTEGER;
    
    IF (param_time < 0 OR param_time > 2359) THEN
    RAISE EXCEPTION -746, 0, "Invalid time format. Should be: 0 - 2359";
    END IF
    
    LET tm_str = LPAD(param_time, 4, 0);
    
    LET h = SUBSTR(tm_str, 1, 2);
    
    IF (h < 0 OR h > 23) THEN
    RAISE EXCEPTION -746, 0, "Invalid time format. Should be: 0 - 2359";
    END IF
    
    LET m = SUBSTR(tm_str, 3, 4);
    
    IF (m < 0 OR m > 59) THEN
    RAISE EXCEPTION -746, 0, "Invalid time format. Should be: 0 - 2359";
    END IF
    
    RETURN TO_DATE(h || ':' || m , '%R');
    
    end procedure
    
    grant
        execute to (group public)
    
    0 讨论(0)
提交回复
热议问题