Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate three columns data into one column in Postgres

Tags:

sql

postgresql

Can anyone tell me which command is used for concatenate three columns data into one column in PostgreSQL database?

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
like image 572
user1522546 Avatar asked Oct 29 '25 17:10

user1522546


1 Answers

Just use concatenation operator || : 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

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

Use this:

select (begin || '-' || month || '-' || year)::date as begin 
from t
like image 72
Michael Buen Avatar answered Oct 31 '25 06:10

Michael Buen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!