将动态日期部分传递给BigQuery中的Date_Add

huangapple go评论61阅读模式
英文:

Passing Dynamic Date Part to Date_Add in BigQuery

问题

有没有办法将一个动态字符串传递给在date_add函数中整数之后的日期部分?

即:

date_add('2023-01-01', interval 7 {dynamic_date_part})

其中{dynamic_date_part}可以是'day'、'week'、'month'等?

我尝试过这样做,但它会拒绝在那里放置的任何内容,只显示"unexpected"。

英文:

Is there a way to pass a dynamic string to the date part that comes after the integer in the date_add function?

I.e.

date_add('2023-01-01', interval 7 {dynamic_date_part})

where {dynamic_date_part} could be 'day', 'week', 'month' etc?

I tried this but it rejects anything that gets put there as just "unexpected"

答案1

得分: 1

你可以使用以下查询来添加动态日期部分:

select date_add(dte, interval n {dynamic_date_part}) as date
from (select current_date as dte, 1 as n union all
      SELECT CURRENT_DATE, 2)

其中,12 在以下部分表示时间间隔的日/周/月:

from (select current_date as dte, 1 as n union all
      SELECT CURRENT_DATE, 2)

以下是查询和输出的图示:
将动态日期部分传递给BigQuery中的Date_Add

英文:

You can use this query to add the dynamic date part:

select date_add(dte, interval n {dynamic_date_part}) as date
from (select current_date as dte, 1 as n union all
      SELECT CURRENT_DATE, 2)

As 1 and 2 in

from (select current_date as dte, 1 as n union all
      SELECT CURRENT_DATE, 2)

represent the day/week/month of interval.

Here is the query and output:
将动态日期部分传递给BigQuery中的Date_Add

huangapple
  • 本文由 发表于 2023年2月10日 04:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定