在 Snowflake/SQL 中舍入 Current_Timestamp 到最近的半小时
Round Down Current_Timestamp to Nearest Half Hour in Snowflake/SQL
我试图将 CURRENT_TIMESTAMP 舍入到 Snowflake 中最接近的半小时。在 SQL 中,下面的代码会将时间戳“2021-01-01 10:35:00”舍入为“2021-01-01 10:30:00”或“2021-01-01 [=18” =]' 到 '2021-01-01 17:00:00',但是,此语法在 Snowflake 中不起作用。有谁知道如何在 Snowflake 中执行此操作?
SELECT DATEADD(mi, DATEDIFF(mi, 0, GETDATE())/30*30, 0)
从纪元转换为秒,除以 1800,四舍五入,并带回时间戳:
select to_timestamp(
round(
date_part(epoch_second, current_timestamp())/1800
)*1800) nearest_half_hour
# 2021-02-27T01:30:00Z
或任何任意时间戳:
select to_timestamp(
round(
date_part(epoch_second, to_timestamp('2020-10-10 17:51:01'))/1800
)*1800) nearest_half_hour
# 2020-10-10T18:00:00Z
Snowflake 中有一个名为 time_slice
的内置函数。
select time_slice(current_timestamp::timestamp_ntz, 30, 'MINUTE');
我试图将 CURRENT_TIMESTAMP 舍入到 Snowflake 中最接近的半小时。在 SQL 中,下面的代码会将时间戳“2021-01-01 10:35:00”舍入为“2021-01-01 10:30:00”或“2021-01-01 [=18” =]' 到 '2021-01-01 17:00:00',但是,此语法在 Snowflake 中不起作用。有谁知道如何在 Snowflake 中执行此操作?
SELECT DATEADD(mi, DATEDIFF(mi, 0, GETDATE())/30*30, 0)
从纪元转换为秒,除以 1800,四舍五入,并带回时间戳:
select to_timestamp(
round(
date_part(epoch_second, current_timestamp())/1800
)*1800) nearest_half_hour
# 2021-02-27T01:30:00Z
或任何任意时间戳:
select to_timestamp(
round(
date_part(epoch_second, to_timestamp('2020-10-10 17:51:01'))/1800
)*1800) nearest_half_hour
# 2020-10-10T18:00:00Z
Snowflake 中有一个名为 time_slice
的内置函数。
select time_slice(current_timestamp::timestamp_ntz, 30, 'MINUTE');