简体   繁体   中英

timestamp() do not take function parameter as input?

I am using sql in postgresql

cast(col as timestamp($1))

It gives me error around parameter

The sql works when I give the number directly:

cast(col as timestamp(1))

So timestamp do not take function parameter?

The precision modifier of the timestamp type is part of the type name and cannot be parameterized in plain SQL. That's not a function, even though the syntax with parentheses looks the same.

You would need dynamic SQL for that. In a PL/pgSQL function build the statement as string and run it with EXECUTE . Something like:

EXECUTE 'SELECT col::timestamp(' || $1 || ')'
INTO my_var;

$1 being type integer . Not a string type like text , or you have a possible SQL injection hole.

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