如何在WHERE子句中用动态日期替换固定日期?

时间:2020-07-14 14:07:38

标签: sql postgresql postgresql-10

select concat(CURRENT_DATE, ' ', '00:00:00+02:00')

返回"2020-07-14 00:00:00+02:00"

我可以将此结果复制粘贴到我的WHERE子句中:

where (xyz between timestamp with time zone '2020-06-01 00:00:00+02:00' 
and timestamp with time ZONE 2020-07-14 00:00:00+02:00

它有效,但是如果我使用更具动态性的结构concat(CURRENT_DATE, ' ', '00:00:00+02:00'),则:

where (xyz between timestamp with time zone '2020-06-01 00:00:00+02:00' 
and timestamp with time ZONE concat(CURRENT_DATE, ' ', '00:00:00+02:00')

我收到语法错误:

错误:“ concat”第57行处或附近的语法错误:... 6-01 00:00:00 + 02:00',时间戳记与时间ZONE concat(CUR ...

为什么第一个WHERE子句起作用,但另一个却不起作用,即使它以相同的格式打印相同的日期呢?

1 个答案:

答案 0 :(得分:2)

您可以使用at time zone

where xyz between ('2020-06-01'::date at time zone '+02:00') and (current_date at time zone '+02:00')
相关问题