简体   繁体   English

如何在Postgresql中将变量传递给date_part函数?

[英]How do I pass a variable into date_part function in postgresql?

I'm passing a variable from my function into the date_part function but I keep getting a syntax error. 我正在将函数中的变量传递给date_part函数,但date_part收到语法错误。 Tried these 2 examples below and still got a syntax error. 在下面尝试了这两个示例,仍然出现语法错误。

Example 1: 范例1:

_month = date_part('month', timestamp NEW."sdate");

Example 2: 范例2:

_month = date_part('month', timestamp format('%s',NEW."sdate"));

Can anyone tell me how do I pass my variable into this function? 谁能告诉我如何将变量传递给此函数?

I think there is no need to use timestamp literal or format the string if you need to pass it to date_part function. 我认为如果需要将timestamp字面量传递给date_part函数,则无需使用timestamp文字或format字符串。 If sdate is of type DATE , you may use it directly. 如果sdateDATE类型,则可以直接使用它。

date_part('month',NEW."sdate");

If sdate is a proper date string such as '2018-01-26 13:15:20', then your first example should work fine - date_part('month', timestamp NEW."sdate"); 如果sdate是正确的日期字符串,例如'2018-01-26 13:15:20',那么您的第一个示例应该可以正常工作date_part('month', timestamp NEW."sdate");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM