当前位置: 动力学知识库 > 问答 > 编程问答 >

sql - Can you concatenate two strings to create dynamic column names in PostgreSQL?

问题描述:

I have a form where people can type in a start and end date, as well as a column name prefix.

In the backend, I want to do something along the lines of

SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS ({{prefix}} + '_startDate')

Is this possible? Basically, I want to dynamically create the name of the new column. The table is immediately returned to the user, so I don't want to mutate the underlying table itself. Thanks!

网友答案:

You can execute dynamic query that you have prepared by using EXECUTE keyword, otherwise it is not possible to have dynamic structure of SQL.

Since you are preparing your SQL outside database, you can use something like:

SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS {{prefix}}_startDate

Assuming that {{prefix}} is replaced with some string by your template before it is sent to database.

分享给朋友:
您可能感兴趣的文章:
随机阅读: