user1522546
user1522546

Reputation: 67

Concatenate three columns data into one column in Postgres

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

Upvotes: 4

Views: 15991

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269553

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()).

Upvotes: 0

Michael Buen
Michael Buen

Reputation: 39393

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

Upvotes: 11

Adriaan Stander
Adriaan Stander

Reputation: 166356

Have a look at 9.4. String Functions and Operators

Upvotes: 2

Related Questions