Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:24:17+00:00 2026-06-11T02:24:17+00:00

In PostgreSQL, I want to use an SQL statement to combine two columns and

  • 0

In PostgreSQL, I want to use an SQL statement to combine two columns and create a new column from them.

I’m thinking about using concat(...), but is there a better way?
What’s the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:24:19+00:00Added an answer on June 11, 2026 at 2:24 am

    Generally, I agree with @kgrittn’s advice. Go for it.

    But to address your basic question about concat(): it is useful if you need to deal with null values – and null has neither been ruled out in your question nor in the one you refer to.

    If you can rule out null values, the good old (SQL standard) concatenation operator || is still the best choice, and @luis’ answer is just fine:

    SELECT col_a || col_b;
    

    If either of your columns can be null, the result would be null in that case. You could defend with COALESCE:

    SELECT COALESCE(col_a, '') || COALESCE(col_b, '');
    

    But that gets tedious quickly with more arguments. That’s where concat() comes in, which never returns null, not even if all arguments are null. The manual:

    NULL arguments are ignored.

    SELECT concat(col_a, col_b);
    

    The remaining corner case for both alternatives is where all input columns are null in which case we still get an empty string ''. To get null instead:

    SELECT CASE
              WHEN col_a IS NULL THEN col_b
              WHEN col_b IS NULL THEN col_a
              ELSE col_a || col_b
           END;
    

    This gets more complex with more columns quickly. Again, use concat(), but add a check for the special condition:

    SELECT CASE WHEN (col_a, col_b) IS NULL THEN NULL
                ELSE concat(col_a, col_b) END;
    

    How does this work?
    (col_a, col_b) is shorthand for ROW (col_a, col_b). And a row type is only null if all columns are null. Detailed explanation:

    • NOT NULL constraint over a set of columns

    Also, use concat_ws() to add separators between elements (ws for "with separator").


    An expression like the one in Kevin’s answer:

    SELECT $1.zipcode || ' - ' || $1.city || ', ' || $1.state;
    

    is tedious to prepare for null values in PostgreSQL 8.3 (without concat()). One way (of many):

    SELECT COALESCE(
             CASE
                WHEN $1.zipcode IS NULL THEN $1.city
                WHEN $1.city    IS NULL THEN $1.zipcode
                ELSE $1.zipcode || ' - ' || $1.city
             END, '')
           || COALESCE(', ' || $1.state, '');
    

    Function volatility is only STABLE

    concat() and concat_ws() are STABLE functions, not IMMUTABLE because they can invoke datatype output functions (like timestamptz_out) that depend on locale settings.
    Explanation by Tom Lane.

    This prohibits their direct use in index expressions. If you know that the result is actually immutable in your case, you can work around this with an IMMUTABLE function wrapper. Example here:

    • Does PostgreSQL support "accent insensitive" collations?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I create a table in PostgreSQL, the SQL I use looks like this:
I want to do something like this in PostgreSQL. I tried this: CREATE or
I want to create a read-only user in PostgreSQL. The intention is to have
I have created a table in postgreSQL. I want to look at the SQL
During the installation of my app, I want to create a PostgreSQL-Database and some
I installed OpenGeo Suite 2.4.5 in windows 7. I want to access postgresql from
I want to create a postgres function that builds the set of columns it
I have a Java application and I want to use SQL database. I have
I want to use sqlite memory database for all my testing and Postgresql for
I want to generate several SQL statements based on a column list using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.