Between Operator Big Query Standard SQL

Between Operator Big Query Standard SQL

在 BQ 中使用标准 SQL - 作为任务的一部分,我想搜索在前一天下午 2 点和当天下午 2 点之间创建的记录

我找到了

SELECT DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 10 hour) 给我昨天下午 2 点

SELECT DATETIME_ADD(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 14 小时)

Gives me 2pm today

所以,我假设我可以在我的查询中使用它

Select * from  
TableA  
where CreatedDate Between  
 DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 10 hour) and DATETIME_ADD(DATETIME_TRUNC(CURRENT_DATETIME(), DAY), INTERVAL 14 hour)

但是我得到以下

No matching signature for operator BETWEEN for argument types: TIMESTAMP, DATETIME, DATETIME. Supported signature: (ANY) BETWEEN (ANY) AND (ANY)

我哪里错了?

你的问题是 CreatedDateTIMESTAMP,你需要转换成 DATETIME

可能是这样的:

where DATETIME(CreatedDate) Between ...

但是您可以轻松地为 TIMESTAMP 编写自己的语句

SELECT timestamp_sub(timestamp_trunc(current_timestamp() , DAY),interval 10 hour)