Why is CONCAT not piecing together strings?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chopin
    New Member
    • Mar 2007
    • 37

    Why is CONCAT not piecing together strings?

    I have a table `city` that contains four cities (a,b,c,d)

    I am trying to concatenate the city with a string. It is important that the city must come first:

    CONCAT(`city`, ' text') should do the trick, but I am getting the following:
    Code:
    a
    b
    c
    d text
    However if I reverse the CONCAT order to CONCAT('text ', `city`), then I get the proper string attachments:
    Code:
    text a
    text b
    text c
    text d
    What does this behavior indicate?
  • Jerry Winston
    Recognized Expert New Member
    • Jun 2008
    • 145

    #2
    I agree, that's the wrong output but what does your SQL statement look like? It's going to be tough to figure out the problem with your query without the query.

    However, I did notice you have something funky going on with the single quote marks around your "text" and "city" string literals. Try deleting the single quotes and reapplying them. It looks like you picked up some odd characters by copy/pasting.

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      It should work. Try CONCAT_WS() instead of CONCAT().

      Code:
      SELECT CONCAT(' ', `city`, 'text') FROM table

      Comment

      • dgreenhouse
        Recognized Expert Contributor
        • May 2008
        • 250

        #4
        You are using backticks "`" as Jerry Winston noted around "city." Change them to either apostrophes or quotation marks.

        Comment

        Working...