Skip site navigation (1) Skip section navigation (2)

Re: MySql Concat function equivalent

From: Thom Brown <thom(at)linux(dot)com>
To: Vaduvoiu Tiberiu <vaduvoiutibi(at)yahoo(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: MySql Concat function equivalent
Date: 2010-11-09 14:17:21
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
On 9 November 2010 13:46, Vaduvoiu Tiberiu <vaduvoiutibi(at)yahoo(dot)com> wrote:

> Is there any way to create a postgresql function that would take unlimited
> parameters like mysql's concat()? Currently my application was calling the
> concat function with parameters comming from php...something like "select
> concat(".implode(',',array)."). So without having a predefined number of
> parameters, I would need a function that could be able to concatenate the
> fields no matter how many they are. Is this possible?
Which version of PostgreSQL are you on?  If you're on >= 8.4 you can use
these to simulate it:

CREATE FUNCTION concat(VARIADIC arr anyarray) RETURNS text AS $$
    SELECT array_to_string($1, '');

CREATE FUNCTION concat_ws(delim text, VARIADIC arr anyarray) RETURNS text AS
    SELECT array_to_string($2, $1);

Then you can run:

SELECT concat(1,2,3);

to get:



SELECT concat_ws('-',1,2,3);

to get:


Note that all elements of the array you're passing in have to be the same
type.  And 9.1 should have this a better version of this functionality built

Hope that helps.

Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

In response to

pgsql-novice by date

Next:From: Mladen GogalaDate: 2010-11-09 14:49:55
Subject: Re: Delete low priority equivalent in postgresql?
Previous:From: Vaduvoiu TiberiuDate: 2010-11-09 13:46:09
Subject: MySql Concat function equivalent

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group