在Snowflake/SQL中舍入Current_Timestamp到最接近的半小时



我正在尝试将CURRENT_TIMESTAMP舍入到Snowflake中最接近的半小时。在SQL中,下面的代码将四舍五入时间戳从'2021-01-01 10:35:00'到'2021-01-01 10:30:00'或'2021-01-01 17:20:00'到'2021-01-01 17:00:00',但是,这种语法在Snowflake中不起作用。有人知道在雪花里怎么做吗?

SELECT DATEADD(mi, DATEDIFF(mi, 0, GETDATE())/30* 30,0)

在Snowflake中有一个内置函数用于此,称为time_slice

select time_slice(current_timestamp::timestamp_ntz, 30, 'MINUTE');

从epoch转换为秒,除以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     

最新更新