Search code examples
sqlpostgresqlstring-concatenation

Concatenating strings in PostgreSQL result


I have an SQL query like this:

SELECT DISTINCT(id) FROM users WHERE ...

and I would like to display the results like that:

user=12355
user=78949
user=9898
user=489891

Basically with "user=" prepended. Is it possible to do this with PostgreSQL? I've tried with STRING_AGG('user=', DISTINCT(id)) but got an error on DISTINCT(id). Any idea?


Solution

  • You should be able to use || for string concatenation:

    SELECT DISTINCT('user=' || id) 
    FROM users 
    WHERE ...
    

    This might be useful as well:

    http://www.postgresql.org/docs/current/static/functions-string.html