将TIME添加到DATETIME值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将TIME添加到DATETIME值相关的知识,希望对你有一定的参考价值。
我可以从文档中看到DATETIME_ADD
仅适用于INT64
值,而不是TIME
对象。
我有一个DATETIME
代表起点,然后是TIME
对象的持续时间
WITH input AS (
SELECT
DATE(2018,03,05) AS start_date,
TIME(5,0,0) AS start_time,
TIME(8,0,0) AS duration
)
SELECT
*,
DATETIME(start_date,start_time) AS start_datetime,
DATETIME_ADD(
DATETIME_ADD(
DATETIME_ADD(
DATETIME(start_date,start_time),
INTERVAL EXTRACT(HOUR FROM duration) HOUR
),
INTERVAL EXTRACT(MINUTE FROM duration) MINUTE
),
INTERVAL EXTRACT(SECOND FROM duration) SECOND
) AS end_datetime
FROM input
有没有更好的方法将TIME
对象的3个值(小时,分钟,秒)添加到给定的DATETIME
对象?
答案
以下是BigQuery Standard SQL
WITH input AS (
SELECT
DATE(2018,03,05) AS start_date,
TIME(5,0,0) AS start_time,
TIME(8,0,0) AS duration
)
SELECT
*,
DATETIME(start_date,start_time) AS start_datetime,
DATETIME_ADD(
DATETIME_ADD(
DATETIME_ADD(
DATETIME(start_date,start_time),
INTERVAL EXTRACT(HOUR FROM duration) HOUR
),
INTERVAL EXTRACT(MINUTE FROM duration) MINUTE
),
INTERVAL EXTRACT(SECOND FROM duration) SECOND
) AS end_datetime,
DATETIME_ADD(
DATETIME(start_date,start_time),
INTERVAL DATETIME_DIFF(DATETIME(start_date,duration), DATETIME(start_date), SECOND) SECOND
) end_datetime_nicer_way
FROM input
以上是关于将TIME添加到DATETIME值的主要内容,如果未能解决你的问题,请参考以下文章
在 Python 中将 N 秒添加到 datetime.time 的标准方法是啥?
mysql中Timestamp,time,datetime 区别??