在Postgresql中将下划线与字符串连接起来

时间:2015-02-23 15:24:21

标签: string postgresql concatenation

我正在尝试建立一个在Postgres 9.3中看起来像这样的字符串。

2015_2_23_10

这是当前年,月,日和小时,以下划线分隔。

这是创建字符串

的查询
select 
    cast(extract(year from now()) as text) || '_' ||
    cast(extract(month from now()) as text) || '_' ||
    cast(extract(day from now()) as text) || '_' ||
    cast(extract(hour from now()) as text);

然而,下划线变成了空格。我也尝试使用E来表示下划线,就像...|| E'_' || ...一样,但这也不起作用。

1 个答案:

答案 0 :(得分:2)

请尝试这种方式。

select to_char(now(), 'yyyy_MM_DD_hh24');

请参阅演示:http://sqlfiddle.com/#!15/cb05e/3

@courtesy a_horse_with_no_name