Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010

    Unanswered: how to concaternate in postgres

    hi all,
    I have a table 'name' having three columns. (first_name,middle_name,last_name).

    The data in the table is as follows.

    first_name middle_name last_name
    millan kumar malla
    debadutta das

    In the second record there is no middle name.
    Now I want to concatenate the first_name,middle_name,last_name.
    So, I write a query
    'select first_name|| ' ' || middle_name|| ' '|| last_name from name'.

    But it returns only 'milan kumar mall'. Since in the second record there is no middle name, it did not concatenate it. It returns a blank row.

    Is there any query to sort it out.


  2. #2
    Join Date
    Nov 2003
    Provided Answers: 23
    Concatenation of NULL values yields a NULL value.
    Use the coalesce() function to convert NULL values to an empty string in your expression

  3. #3
    Join Date
    Jul 2010
    thanks for your quick response buddy.

  4. #4
    Join Date
    Aug 2010

    full syntax-- try this.

    firstname middlename lastname
    --------- ---------- ----------
    raj kumar shaw
    santosh kumar shaw
    sunny agarwal
    vicky mehta

    select coalesce (firstname, ' ')||' '|| coalesce(middlename, ' ')||' '|| coalesce(lastname, ' ') from tablename;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts