将MSSS固定号码转换为ANSI时间“ hh:mm:ss AM / PM”

时间:2019-05-01 23:38:40

标签: sql date time ansi snowflake

我正在尝试将固定数字转换为不带hh:mm:ss AM/PM日期的时间-因此67683000将是06:48:03 PM22814000 = 06:20:14 AM。我有% 43200000要计算为余数。我的MSSS SQL代码(光标)如下。在Snowflake中,我的代码(不作为游标)需要转换为ANSI,但出现错误,无法弄清原因。

HH:MM:SS AM (or PM)中的预期结果


MSSS - correct

`CONVERT(VARCHAR, DATEADD(ms, AS_AIRED_START_TIME % 43200000, 0), 108)  
+ '' ''  
+ CASE  WHEN AS_AIRED_START_TIME % 86400000 > 43199999       
THEN ''PM''  
ELSE ''AM'' END 

[播出时间]`


ANSI - wrong

`TO_VARCHAR(255), (DATE_PART(MILLISECOND, AS_AIRED_START_TIME % 43200000, 0), 108)  
+ ' '  
+ CASE  WHEN AS_AIRED_START_TIME % 86400000 > 43199999   
THEN 'PM'  
ELSE 'AM' END     

为AiredTime`


ANSI - close but not exact

`TO_CHAR(TRUNC(fed.AS_AIRED_START_TIME/3600),'FM9999999999900')      
|| ':'   
|| TO_CHAR(TRUNC(MOD(fed.AS_AIRED_START_TIME,3600)/60),'FM00')    
|| ':'   
|| TO_CHAR(MOD(fed.AS_AIRED_START_TIME,60),'FM00')` 

Result: 17248:36:40

ANSI - wrong

`TO_TIMESTAMP_NTZ(fed.AS_AIRED_START_TIME,0)` 

ANSI - wrong

`TO_VARCHAR(DATEADD(millisecond,500, current_timestamp),'hh:mi:ss')`

Excel - conversion

enter image description here

所以给定1800000,我将计算为1800000/1000 / 60sec / 60min / 24hours = 00:30:00 = 30分钟

1 个答案:

答案 0 :(得分:1)

with TEST_DATA
  as (
    select 67683000::integer as AS_AIRED_START_TIME -- PM
    union all
    select 24483000::integer -- AM
)
select to_char(timeadd(ms, AS_AIRED_START_TIME, to_time('00:00:00')), 'HH12:MI:SS AM')
from TEST_DATA;