#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0

    Concatenate two columns


    Hi guys,
    I have a little problem. How can I concatenate two columns with PostgreSQL? I have done yet with mysql but now i have to do the same with PostgreSQL.
    This is the instruction that I've used for Mysql:

    UPDATE table SET col1 = concat (col1,'_',col2);

    How can I convert this instruction in PostgreSQL?
    Thanks so much!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,788
    Rep Power
    349
    Why don't you start by reading the manual: http://www.postgresql.org/docs/current/static/functions-string.html

    It clearly shows you that the Postgres statement is the same.

    (Please format your SQL in the future).
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
    http://forums.devshed.com/misc.php?do=bbcode#code

    Tips on how to ask better questions:
    http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    pro tip: If in doubt, try it out.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by shammat
    Why don't you start by reading the manual:

    It clearly shows you that the Postgres statement is the same.

    (Please format your SQL in the future).
    THanks for the answer. Sorry if I haven't formatted my code but it's my first thread. I have read the manual yet but I can't understand very well.
    There is written how to concatenate two strings or some arguments. But How I can do for two columns? Have I use the function "concat"?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    pro tip: If in doubt, try it out.
    I'm trying but with no results
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,788
    Rep Power
    349
    Originally Posted by esaurito
    There is written how to concatenate two strings or some arguments. But How I can do for two columns?
    Two strings or two columns doesn't make a difference, and if you read the link I posted you will see that there is a concat function.


    I'm trying but with no results
    Then you should have shown us what you tried.

    Because the statement you posted will work without any changes in Postgres just as well.
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
    http://forums.devshed.com/misc.php?do=bbcode#code

    Tips on how to ask better questions:
    http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by shammat
    Two strings or two columns doesn't make a difference, and if you read the link I posted you will see that there is a concat function.


    Then you should have shown us what you tried.

    Because the statement you posted will work without any changes in Postgres just as well.
    If I execute this code

    Code:
    UPDATE table SET col1 = concat (col1,'_',col2)
    I haven't the same result like mysql. I want, as result, that colonna1 has the the values of col1 and col2, separated by a "_".
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,788
    Rep Power
    349
    Originally Posted by esaurito
    I haven't the same result like mysql.
    Don't you think you should tell us what result you expect and what you get?

    I want, as result, that colonna1 has the the values of col1 and col2, separated by a "_".
    That is precisely what the statement does.

    See this example: http://sqlfiddle.com/#!12/e1603/1
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
    http://forums.devshed.com/misc.php?do=bbcode#code

    Tips on how to ask better questions:
    http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    10
    Rep Power
    0
    Originally Posted by shammat
    Don't you think you should tell us what result you expect and what you get?

    That is precisely what the statement does.

    See this example:
    Thank you so much! I don't know where i have made some mistakes but now it works!
    I'm in debt with you

IMN logo majestic logo threadwatch logo seochat tools logo