将伪24小时时间转换为12小时时钟时间

时间:2013-02-27 00:50:53

标签: sql ms-access

我有一个MS Access查询,显示订单周转时间。两个问题:

  1. 时间是24小时制时间(例如:0105)所以当您从CPOE时间中减去验证时间时,您得到的值既不是几分钟也不是几小时,所以您不能简单地将其转换为分钟(即:0701 - 0527 = 174,真正的差异是94分钟),
  2. 如果CPOE时间在午夜之前且验证时间在午夜之后,则差异非常扭曲(即:2351 - 0024 = 2327,实际差异为33分钟)。
  3. 任何人都知道如何解决这个问题? 这是SQL查询:

     SELECT 
        BI.PTNAME, 
        PHM_POE_HIST.FUNCTION, 
        PHM_ORDERS.VALIDATED_DATE, 
        PHM_ORDERS.VALIDATED_BY, 
        SAT.USER_NAME, 
        PHM_ORDERS.VALIDATED_TIME, 
        Right([TRAN_DATE_TIME],4) AS CPOE_TIME, 
        Abs([validated_time]-[CPOE_TIME]) AS Diff, 
        PHM_POE_HIST.TRAN_DATE_TIME, 
        PHM_POE_HIST.EFF_DATE_TIME, 
        PHM_POE_HIST.HIST_SEQ, BI.NRS_STATION, 
        SITE.NAME, PHM_ORDERS.PMP, 
        PHM_ORDERS.PRIORITY, 
        Trim([GENERIC_NAME] & " " & [STRENGTH] & " " & [STRENGTH_UNIT] & " " & [DOSAGE_FORM] & " " & [LATIN_DIR_ABBR]) AS DESCRIPTION 
    FROM 
        ((BI LEFT JOIN 
                (PHM_ORDERS LEFT JOIN PHM_POE_HIST ON (PHM_ORDERS.POE_ORDER_NUMBER = PHM_POE_HIST.POE_ORDER_NUMBER) AND (PHM_ORDERS.ITN = PHM_POE_HIST.ITN))
            ON BI.ITN = PHM_ORDERS.ITN) 
        LEFT JOIN SAT
             ON PHM_ORDERS.VALIDATED_BY = SAT.USER_INITIALS)
        LEFT JOIN SITE 
            ON BI.SITE = SITE.SITE
    WHERE 
        (((BI.PTNAME) Is Not Null) 
        AND ((PHM_POE_HIST.FUNCTION)="NW") 
        AND ((PHM_ORDERS.VALIDATED_DATE)=datereltoday(-1)) 
        AND ((PHM_ORDERS.PRIORITY)="STAT"));
    

1 个答案:

答案 0 :(得分:4)

不要在日期或时间做数学......永远。军事时间是一种视觉格式,因此解析小时和分钟:

假设数据类型为char(4)

LEFT(MilitaryTimeColumn, 2) => hours
RIGHT(MilitaryTimeColumn, 2) => minutes

然后转换为数字

CInt(LEFT(MilitaryTimeColumn, 2)) --> hours as integers
CInt(RIGHT(MilitaryTimeColumn, 2)) --> minutes as integers

计算总分钟数

CInt(LEFT(MilitaryTimeColumn, 2)) * 60 + CInt(RIGHT(MilitaryTimeColumn, 2))

然后添加到原始日期

DateAdd("n", 
    CInt(LEFT(MilitaryTimeColumn, 2)) * 60 + CInt(RIGHT(MilitaryTimeColumn, 2)), 
    DateColumn) --> Complete datetime

然后使用DateDiff获取日期之间的距离。