要显示的 psql 格式日期 "week W"

psql format date to display "week W"

根据 Postgres 的数据类型格式化文档 https://www.postgresql.org/docs/9.1/static/functions-formatting.html

W 将允许它显示月中的星期 (1-5)。 但是有没有办法让我在字符串 "week"?

中转义 "w"

所以我可以指定 "\week W",并且 它显示 "week 1"、"week 2" 等

我知道之后可以进行字符串连接,但希望尽可能避免不必要的复杂性。

你可以使用这个:

SELECT TO_CHAR(current_timestamp, '"week" w');

您可以添加任何 text/Character 文字,在您的格式模型中用双引号括起来。

Usage notes for date/time formatting:

......

Ordinary text is allowed in to_char templates and will be output literally. You can put a substring in double quotes to force it to be interpreted as literal text even if it contains pattern key words. For example, in '"Hello Year "YYYY', the YYYY will be replaced by the year data, but the single Y in Year will not be. In to_date, to_number, and to_timestamp, double-quoted strings skip the number of input characters contained in the string, e.g. "XX" skips two input characters.