关于postgresql:ERROR:类型为timestamp的无效输入语法:

ERROR: invalid input syntax for type timestamp:

我在PostgreSQL查询中遇到一个错误:错误:类型为timestamp的输入语法无效:

我正在使用\\set设置值。

1
2
3
\\SET dueDateEarliest '2018-04-01'
\\SET dueDateLatest '2018-08-01'
\\SET dueDateLatest '2018-08-01'

并尝试在我的查询中使用这些值,如下所示:

1
SELECT DISTINCT(bu.id) AS"user_id",c.organization_name AS"name",round(i.balance,2) AS"amount_due",i.id AS"invoice_number",i.due_date AS"due_date",CONCAT('collectionMonth', LPAD(cf2.content,2,'0')) AS"collection_date" FROM base_user bu,contact c,contact_field cf, invoice i, contact_field cf2 WHERE bu.id = c.user_id AND bu.deleted = 0 AND cf.contact_id = c.id AND cf.type_id = 7 AND cf.content = 'DD' AND i.user_id = bu.id AND i.balance > 0 AND i.is_review != 1 AND i.deleted != 1 AND due_date BETWEEN 'dueDateEarliest' AND 'dueDateLatest' AND cf2.contact_id = c.id AND cf2.type_id = 8 ORDER BY bu.id LIMIT 20;

这给出的错误为ERROR:类型为timestamp的输入语法无效:

我没有任何方法来修复它。我使用\\set设置值的方式很好吗?还是应该使用SET来设置值。因为实际上当我不得不从shell脚本运行这些命令时,我会从shell脚本调用/设置为set dueDateEarliest'$ dueDateEarliest'`。

哪个是最好的方法?

还要附加屏幕截图enter image description here


这是如何格式化查询的问题。让我们简化一下:

1
2
3
4
# \\SET dueDateEarliest '2018-04-01'
# SELECT 'dueDateEarliest'::timest
ERROR:  invalid INPUT syntax FOR TYPE TIMESTAMP:"dueDateEarliest"
LINE 1: SELECT 'dueDateEarliest'::timest

它不起作用,因为它试图使用字符串'dueDateEarliest',而不是变量。

这是正确的方法:

1
2
3
4
5
# SELECT :'dueDateEarliest'::timest
      TIMESTAMP
---------------------
 2018-04-01 00:00:00
(1 ROW)