将 MSSS 固定数字转换为 ANSI 时间 "hh:mm:ss AM/PM"
Convert MSSS Fixed Number to ANSI Time "hh:mm:ss AM/PM"
我正在尝试将固定数字转换为没有日期的时间,如 hh:mm:ss AM/PM
- 因此 67683000
将是 06:48:03 PM
; 22814000
= 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 分钟
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;
我正在尝试将固定数字转换为没有日期的时间,如 hh:mm:ss AM/PM
- 因此 67683000
将是 06:48:03 PM
; 22814000
= 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 分钟
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;