【问题标题】:How to insert dates as date array in postgresql如何在postgresql中插入日期作为日期数组
【发布时间】:2021-12-29 02:18:09
【问题描述】:

我正在尝试通过选择查询将日期添加到数组中。

我收到此错误:

SQL 错误 [22P02]:错误:格式错误的数组文字:“2021-04-02”

declare dateval date[];
begin 
select days into dateval from holidays where days between '2021-01-01' and '2021-12-31' and city='NY';

那么我需要在 if 语句中比较这个数组 我在这个 dateval 数组中的日期变量 做某事或去其他类似的事情

【问题讨论】:

    标签: arrays postgresql date plpgsql


    【解决方案1】:

    您需要将它们聚合到一个数组中才能将它们存储到一个数组中:

    select array_agg(days) 
      into dateval
    from ...
    

    【讨论】:

      【解决方案2】:

      也许您不需要数组,但在您的 if 条件中使用 exists 和子查询。

      if my_date_var between '2021-01-01' and '2021-12-31' and exists 
      (
        select from holidays 
        where my_date_var = days 
        and city='NY'
      ) then ...
      

      或使用in(可能效率较低)

      if my_date_var in
      (
        select days from holidays 
        where days between '2021-01-01' and '2021-12-31' 
        and city='NY'
      ) then ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-18
        • 1970-01-01
        • 2020-06-07
        • 2020-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多