“Postgre”是日期类型,但表达式是文本类型。

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

Postgre, is of type date but expression is of type text

问题

I have error

错误: 列 "portfolio_end_date" 的类型为日期,但表达式的类型为文本
第 5 行: portfolio_end_date = tmp.portfolio_end_date,
^
提示: 您需要重新编写或转换表达式。
SQL 状态: 42804
字符: 186

为什么PostgreSQL返回错误

  1. UPDATE ods2.project_portfolios as tbl
  2. set id_portfolio = tmp.id_portfolio,
  3. id_project_link = tmp.id_project_link,
  4. portfolio_start_date = tmp.portfolio_start_date,
  5. portfolio_end_date = tmp.portfolio_end_date,
  6. id_employee = tmp.id_employee,
  7. portfolio_changed_date = tmp.portfolio_changed_date,
  8. portfolio_quarter = tmp.portfolio_quarter,
  9. end_month = tmp.end_month,
  10. historicity_time = tmp.historicity_time
  11. from (
  12. values (
  13. 491533570142,
  14. 25,
  15. '2022-07-01'::date,
  16. NULL,
  17. 51,
  18. '2023-02-28 14:27:24'::timestamp,
  19. NULL,
  20. '2022-07-01'::date,
  21. '2023-03-20 23:00:16'::timestamp)
  22. ) as tmp(
  23. id_portfolio, id_project_link, portfolio_start_date, portfolio_end_date, id_employee,
  24. portfolio_changed_date, portfolio_quarter, end_month, historicity_time)
  25. where tbl.id_portfolio = tmp.id_portfolio and tbl.historicity_time between
  26. '2023-05-25 00:00:00' and '2023-05-25 23:59:59';

请求失败,尽管我看不到任何错误。

英文:

i have error

ERROR: column "portfolio_end_date" is of type date but expression is of type text
LINE 5: portfolio_end_date = tmp.portfolio_end_date,
^
HINT: You will need to rewrite or cast the expression.
SQL state: 42804
Character: 186

Why potgre retrun error

  1. UPDATE ods2.project_portfolios as tbl
  2. set id_portfolio = tmp.id_portfolio,
  3. id_project_link = tmp.id_project_link,
  4. portfolio_start_date = tmp.portfolio_start_date,
  5. portfolio_end_date = tmp.portfolio_end_date,
  6. id_employee = tmp.id_employee,
  7. portfolio_changed_date = tmp.portfolio_changed_date,
  8. portfolio_quarter = tmp.portfolio_quarter,
  9. end_month = tmp.end_month,
  10. historicity_time = tmp.historicity_time
  11. from (
  12. values (
  13. 491533570142,
  14. 25,
  15. '2022-07-01'::date,
  16. NULL,
  17. 51,
  18. '2023-02-28 14:27:24'::timestamp,
  19. NULL,
  20. '2022-07-01'::date,
  21. '2023-03-20 23:00:16'::timestamp)
  22. ) as tmp(
  23. id_portfolio, id_project_link, portfolio_start_date, portfolio_end_date, id_employee,
  24. portfolio_changed_date, portfolio_quarter, end_month, historicity_time)
  25. where tbl.id_portfolio = tmp.id_portfolio and tbl.historicity_time between
  26. '2023-05-25 00:00:00' and '2023-05-25 23:59:59';

Request fails although I don't see any errors in it

答案1

得分: 0

If an element in a VALUES statement is not a number and is not explicitly cast to a different data type, it will resolve as type text:

  1. SELECT pg_typeof(col)
  2. FROM (VALUES (NULL)) AS v(col);
  3. pg_typeof
  4. ═══════════
  5. text
  6. (1 row)

Now there is no assignment cast between text and date:

  1. \dC date
  2. List of casts
  3. Source type Target type Function Implicit?
  4. ═════════════════════════════╪═════════════════════════════╪═════════════╪═══════════════
  5. date timestamp with time zone timestamptz yes
  6. date timestamp without time zone timestamp yes
  7. timestamp with time zone date date in assignment
  8. timestamp without time zone date date in assignment
  9. (4 rows)

Therefore you get the error you observe.

Add an explicit type cast:

  1. UPDATE ods2.project_portfolios
  2. SET portfolio_end_date = tmp.portfolio_end_date, ...
  3. FROM (VALUES (CAST (NULL AS date), ...)) AS tmp (portfolio_end_date, ...)
  4. WHERE ...;
英文:

If an element in a VALUES statement is not a number and is not explicitly cast to a different data type, it will resolve as type text:

  1. SELECT pg_typeof(col)
  2. FROM (VALUES (NULL)) AS v(col);
  3. pg_typeof
  4. ═══════════
  5. text
  6. (1 row)

Now there is no assignment cast between text and date:

  1. \dC date
  2. List of casts
  3. Source type Target type Function Implicit?
  4. ═════════════════════════════╪═════════════════════════════╪═════════════╪═══════════════
  5. date timestamp with time zone timestamptz yes
  6. date timestamp without time zone timestamp yes
  7. timestamp with time zone date date in assignment
  8. timestamp without time zone date date in assignment
  9. (4 rows)

Therefore you get the error you observe.

Add an explicit type cast:

  1. UPDATE ods2.project_portfolios
  2. SET portfolio_end_date = tmp.portfolio_end_date, ...
  3. FROM (VALUES (CAST (NULL AS date), ...)) AS tmp (portfolio_end_date, ...)
  4. WHERE ...;

huangapple
  • 本文由 发表于 2023年5月25日 23:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334234.html
匿名

发表评论

匿名网友

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

确定