postgresql 在 Postgres 中将三列数据连接成一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11464138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate three columns data into one column in Postgres
提问by user1522546
Can anyone tell me which command is used for concatenate three columns data into one column in PostgreSQL database?
谁能告诉我哪个命令用于将 PostgreSQL 数据库中的三列数据连接成一列?
e.g.
例如
If the columns are
如果列是
begin | Month | Year
12 | 1 | 1988
13 | 3 | 1900
14 | 4 | 2000
15 | 5 | 2012
result like
结果就像
Begin
12-1-1988
13-3-1900
14-4-2000
15-5-2012
回答by Michael Buen
Just use concatenation operator ||
: http://www.sqlfiddle.com/#!1/d66bb/2
只需使用连接运算符||
:http: //www.sqlfiddle.com/#!1 / d66bb/2
select begin || '-' || month || '-' || year as begin
from t;
Output:
输出:
| BEGIN |
-------------
| 12-1-1988 |
| 13-3-1900 |
| 14-4-2000 |
| 15-5-2012 |
If you want to change the begin column itself, begin column must be of string type first, then do this: http://www.sqlfiddle.com/#!1/13210/2
如果要更改开始列本身,则开始列必须首先是字符串类型,然后执行以下操作:http: //www.sqlfiddle.com/#!1/13210/2
update t set begin = begin || '-' || month || '-' || year ;
Output:
输出:
| BEGIN |
-------------
| 12-1-1988 |
| 13-3-1900 |
| 14-4-2000 |
| 15-5-2012 |
UPDATE
更新
About this:
对这个:
but m not getting null value column date
but m not getting null value column date
Use this:
用这个:
select (begin || '-' || month || '-' || year)::date as begin
from t
回答by Adriaan Stander
Have a look at 9.4. String Functions and Operators
回答by Gordon Linoff
This is an old post, but I just stumbled upon it. Doesn't it make more sense to create a date data type? You can do that using:
这是一个旧帖子,但我只是偶然发现了它。创建日期数据类型不是更有意义吗?你可以使用:
select make_date(year, month, begin)
A date seems more useful than a string (and you can even format it however you like using to_char()
).
日期似乎比字符串更有用(您甚至可以随意使用 格式化它to_char()
)。