BigQuery timestamp current date minus x days
Asked Answered
T

2

5

I have a TIMESTAMP column with dates that I need to filter. I need to grab the data that is 5 days old. So current date - 5 days. My data is in BigQuery. I tried the following query:

where created_time >= (TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), DAY), INTERVAL -5 DAY)

I got an error: Unexpected INTERVAL expression

Translative answered 7/4, 2021 at 16:39 Comment(0)
E
5

You are missing TIMESTAMP_ADD():

where created_time > TIMESTAMP_ADD(TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), DAY), INTERVAL -5 DAY)
Eruptive answered 7/4, 2021 at 16:42 Comment(1)
Worked like a charm! Thank you, I have accepted the answer.Translative
D
1

It's also possible to use TIMESTAMP_SUB()

where created_time > TIMESTAMP_SUB(TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), DAY), INTERVAL 5 DAY)
Diagonal answered 16/7 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.