简体   繁体   中英

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. Tried these 2 examples below and still got a syntax error.

Example 1:

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

Example 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. If sdate is of type DATE , you may use it directly.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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