代码之家  ›  专栏  ›  技术社区  ›  Jakub Szlaur

如何在查询中将BIGINT转换为TIMESTAMPZ?

  •  0
  • Jakub Szlaur  · 技术社区  · 3 年前

    使用:

    • Suabase中的PostgreSQL 14
    • 格拉法纳云

    我正在尝试转换 BIGINT 时间戳(以毫秒为单位) TIMESTAMPZ 在里面 PostgreSQL 14

    这个 BIGINT 是存储在 $__to $__from 。我正在尝试使用此查询查询特定时间范围内的数据:

    SELECT
      "timestamp" AS "time",
      etalon,
      humidity,
      temperature
    FROM "values"
    WHERE
      timestamp >= TO_TIMESTAMP($__from, 'DD/MM/YYYY HH24:MI:SS')
      and timestamp <  TO_TIMESTAMP($__to, 'DD/MM/YYYY HH24:MI:SS')
    

    上面的查询导致了此错误:

    function to_timestamp(bigint, unknown) does not exist
    

    我已经研究了这些主题,但找不到可行的解决方案:

    1. Postgres timestamp to unix time in milliseconds as a bigint
    2. https://dba.stackexchange.com/questions/215354/convert-date-format-into-bigint-format-in-postgresql
    3. How to format bigint field into a date in Postgresql?

    编辑

    使用 Quassnoi 解决方案也不起作用:

    SELECT
      "timestamp" AS "time",
      etalon,
      humidity,
      temperature
    FROM "values"
    WHERE
      timestamp >= TO_CHAR(TO_TIMESTAMP(1644770125499 / 1000), 'DD/MM/YYYY HH24:MI:SS')
      and timestamp <  TO_CHAR(TO_TIMESTAMP(1644770125499 / 1000), 'DD/MM/YYYY HH24:MI:SS')
    

    结果:

    operator does not exist: timestamp with time zone >= text
    

    使用评论中的建议,我确实转换了BIGINT,但我得到了看起来很奇怪的时间戳:

    enter image description here

    我的时间戳栏的类型:

    enter image description here

    1 回复  |  直到 3 年前
        1
  •  1
  •   Laurenz Albe    3 年前

    你必须使用的单参数形式 to_timestamp :

    SELECT to_timestamp(1644853209.6);
    
           to_timestamp       
    ══════════════════════════
     2022-02-14 16:40:09.6+01
    (1 row)