PostgreSQL如何连接间隔值'2天'
在 PostgreSQL 中,我想将 current_timestamp
与 interval
连接起来,如下所示:
select current_timestamp + interval 2||' days'
但是当我这样做时,我收到错误:
[Err] ERROR: syntax error at or near "2"
LINE 1: select current_timestamp + interval 2||' days'
但是如果我这样做,它会正常工作:
select current_timestamp + interval '2 days'
为什么其中一个有效,而另一个却不起作用?
参考下页 http://www.postgresql.org/docs/8.0/static/functions-datetime.html< /a>
In PostgreSQL I want to concat the current_timestamp
with an interval
as follows:
select current_timestamp + interval 2||' days'
But when I do, I get an error:
[Err] ERROR: syntax error at or near "2"
LINE 1: select current_timestamp + interval 2||' days'
But if I do it like this, it works correctly:
select current_timestamp + interval '2 days'
Why does one work, but not the other?
With reference to the following page
http://www.postgresql.org/docs/8.0/static/functions-datetime.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
部分问题在于间隔的标准 SQL 表达式引用了数字,但没有引用关键字。所以你必须小心。
在 PostgreSQL 中,像“2 day”和“2 days”这样的引用也有效。所以你可能会认为'2'|| “天”是等价的,但事实并非如此。
正如 AH 所说,解决方案是将结果字符串转换为区间。
您还可以使用变量代替 2。这会生成 2012 年的日历。
我使用最终的日期转换,因为日期 + 间隔返回时间戳。
Part of the problem is that the standard SQL expression for intervals quotes the number, but not the keywords. So you have to be careful.
In PostgreSQL, quoting like '2 day' and '2 days' also works. So you might think that '2' || ' days' would be equivalent, but it's not.
The solution, as A.H. said, is to cast the result string as an interval.
You can also use a variable in place of 2. This generates a calendar for 2012.
I use that final cast to date, because date + interval returns a timestamp.
请尝试以下语法:
或者甚至是这个:
Please try this syntax:
or even this one:
我使用这个
无联系函数
I use this
no concact function